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



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

相关推荐
问简3 分钟前
虚拟化对比
服务器
0xDevNull10 分钟前
MySQL数据冷热分离详解
后端·mysql
AI袋鼠帝18 分钟前
OpenClaw(龙虾)最强开源对手!Github 40K Star了,又一个爆火的Agent..
后端
lly20240627 分钟前
C 标准库 - `<stdio.h>`
开发语言
航Hang*27 分钟前
Windows Server 配置与管理——第3章:文件系统管理
运维·服务器·windows·vmware
沫璃染墨29 分钟前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++
jwn99929 分钟前
Laravel6.x核心特性全解析
开发语言·php·laravel
迷藏49431 分钟前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
lifewange1 小时前
Linux ps 进程查看命令详解
linux·运维·服务器