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



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

相关推荐
RisunJan几秒前
Linux命令-lprm(删除打印队列中任务)
linux·运维·服务器
Ronin3051 分钟前
持久化数据管理中心模块
开发语言·c++·rabbitmq·gtest
froginwe112 分钟前
AJAX 实例详解
开发语言
魔力军2 分钟前
Rust学习Day2: 变量与可变性、数据类型和函数和控制流
开发语言·学习·rust
云姜.3 分钟前
TCP协议特性
服务器·网络·tcp/ip
zzzsde3 分钟前
【Linux】进程(5):命令行参数和环境变量
linux·运维·服务器
sycmancia4 分钟前
C++——强制类型转化、const的理解
开发语言·c++
hzb666665 分钟前
unictf2026
开发语言·javascript·安全·web安全·php
计算机小手5 分钟前
Docker 部署 OpenClaw 汉化版,畅玩个人 AI 智能代理
经验分享·docker·aigc·开源软件
燃于AC之乐6 分钟前
深入解剖STL deque:从源码剖析到容器适配器实现
开发语言·c++·stl·源码剖析·容器实现