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



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

相关推荐
ServBay1 小时前
Claude Fable 5.5 测试曝光,但它在悄悄给你打分
后端·ai编程·claude
1-1=01 小时前
C# 委托与事件
开发语言·c#
Fuyo_11191 小时前
C++ 运行时多态与虚函数机制全面剖析【面试重点】
开发语言·c++·面试
番茄啊1 小时前
linux 开启xftp连接
linux·运维·服务器
w_zero_one2 小时前
递归算法(一)
java·开发语言·算法
IT规划师2 小时前
一文讲透五种发布机制:滚动、蓝绿、金丝雀、红黑与 AB
后端·面试
Julien20043 小时前
管理机密(一)
运维·服务器·开发语言·学习方法
Chen_harmony3 小时前
七、STL与string类
开发语言·c++
沐晓时光3 小时前
C语言入门,字符串和字符串函数知识讲解
c语言·开发语言
一条Go3 小时前
两套权限目录,三重闸门:Alioth 的多租户 RBAC 为什么这么拧
后端·程序员·github