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



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

相关推荐
短剑重铸之日几秒前
《7天学会Redis》特别篇:Redis十大经典面试题2
数据库·redis·后端·缓存·架构
草莓熊Lotso几秒前
Linux 命令行参数与环境变量实战:从基础用法到底层原理
linux·运维·服务器·开发语言·数据库·c++·人工智能
枫叶丹42 分钟前
【Qt开发】Qt系统(七)-> Qt网络安全
c语言·开发语言·c++·qt·网络安全
草莓熊Lotso6 分钟前
Qt 控件核心入门:从基础认知到核心属性实战(含资源管理)
运维·开发语言·c++·人工智能·后端·qt·架构
@CLoudbays_Martin111 小时前
SDK游戏盾的工作原理具体是怎么完成防护的?
服务器·网络·安全·游戏
2501_945837431 小时前
全栈AI融合,阿里云Qwen3架构重塑云服务器算力形态
服务器
曹轲恒8 小时前
Java中断
java·开发语言
Grassto8 小时前
深入 `modload`:Go 是如何加载并解析 module 的
golang·go·go module
施棠海9 小时前
监听与回调的三个demo
java·开发语言
時肆4859 小时前
C语言造轮子大赛:从零构建核心组件
c语言·开发语言