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



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

相关推荐
xuhaoyu_cpp_java6 小时前
项目学习(三)分页查询
java·经验分享·笔记·学习
想吃火锅10056 小时前
【leetcode】405.数字转换为十六进制数js
开发语言·javascript·ecmascript
HackTwoHub6 小时前
最新Nessus2026.6.8版本主机漏洞扫描/探测工具Windows/Linux
linux·运维·服务器·安全·web安全·网络安全·安全架构
专注VB编程开发20年6 小时前
AI 生成C# WinForm 窗体 = 目前就是垃圾
开发语言·人工智能·c#
cfm_29146 小时前
JVM GC垃圾回收初步了解
java·开发语言·jvm
QWEDDRFTG7 小时前
C13/C19怎么选?服务器电源线电流与接口选型技巧
服务器
~小先生~7 小时前
Python从入门到放弃(一)
开发语言·python
许彰午7 小时前
17_synchronized关键字深度解析
java·开发语言
z落落7 小时前
C# 泛型接口和泛型类+泛型约束
开发语言·c#
阿正的梦工坊7 小时前
【Rust】02-变量、不可变性与基础类型
开发语言·后端·rust