GoLang并发示例代码2(关于逻辑处理器运行顺序)

文章目录

go 复制代码
// This sample program demonstrates how the goroutine scheduler
// will time slice goroutines on a single thread.
package main

import (
	"fmt"
	"runtime"
	"sync"
)

// wg is used to wait for the program to finish.
var wg sync.WaitGroup

// main is the entry point for all Go programs.
func main() {
	// Allocate 1 logical processors for the scheduler to use.
	runtime.GOMAXPROCS(1)

	// Add a count of two, one for each goroutine.
	wg.Add(2)

	// Create two goroutines.
	fmt.Println("Create Goroutines")
	go printPrime("A")
	go printPrime("B")

	// Wait for the goroutines to finish.
	fmt.Println("Waiting To Finish")
	wg.Wait()

	fmt.Println("Terminating Program")
}

// printPrime displays prime numbers for the first 5000 numbers.
func printPrime(prefix string) {
	// Schedule the call to Done to tell main we are done.
	defer wg.Done()

	// next 是一个标签(label),给外层 for 起了个名字
next:
	for outer := 2; outer < 5000; outer++ {
		for inner := 2; inner < outer; inner++ {
			if outer%inner == 0 {
				continue next
			}
		}
		fmt.Printf("%s:%d\n", prefix, outer)
	}
	fmt.Println("Completed", prefix)
}
bash 复制代码
root@GoLang:~/proj1/GoDistributeCache# go run /root/proj1/GoDistributeCache/example/test.go



之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
vivo互联网技术3 分钟前
从 10 分钟到 1 秒:ES 深度分页任意跳页的三轮优化实战
服务器·数据库·redis·elasticsearch·深度分页
咕白m6259 分钟前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python
云技纵横28 分钟前
@Transactional 里套 REQUIRES_NEW,为什么会把连接池耗尽?
后端·面试
tcdos34 分钟前
不止扫码 — 微信生态深度融合(登录 + 支付 + 消息)
后端·微信小程序
程序员cxuan37 分钟前
Anthropic 大面积封号,连大 V 都忍不了开喷了。
人工智能·后端·程序员
MacroZheng1 小时前
短短几天,暴涨2.8万Star!又一款编程神器开源!
java·人工智能·后端
PinkSun1 小时前
平台线程池用了3年很顺手,换成虚拟线程后我后悔了
后端
达达尼昂1 小时前
Claude : 如何设计可控的agent-loops
前端·人工智能·后端
lambdax1 小时前
Celery 心跳任务内存膨胀排查与修复全记录
后端