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



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

相关推荐
上弦月-编程5 分钟前
Java类与对象:编程核心解密
java·开发语言·jvm
小钻风33665 分钟前
Spring Boot WebSocket 两种集成方式深度解析
spring boot·后端·websocket
土星云SaturnCloud9 分钟前
土星云AI边缘计算-算法运行环境搭建:Docker部署全流程实操
服务器·人工智能·docker·ai·边缘计算
IT_陈寒12 分钟前
Vite热更新失效?我在这坑里卡了一下午
前端·人工智能·后端
大大杰哥14 分钟前
从 Volatile 到 ThreadLocal:Java 线程安全机制备忘
java·开发语言·jvm
云烟成雨TD19 分钟前
Spring AI Alibaba 1.x 系列【55】Interrupts 中断机制:静态中断源码分析
人工智能·后端·spring
AI人工智能+电脑小能手20 分钟前
【大白话说Java面试题 第67题】【JVM篇】第27题:生产环境服务器变慢,诊断思路和性能评估谈谈?
java·服务器·jvm·面试
崇山峻岭之间22 分钟前
matlab绘制复杂曲线
开发语言·matlab
skywalk816323 分钟前
中文编程语言的开创性语法,言律:一门以汉语为思维内核的原生中文编程语言
开发语言·编程
曾阿伦24 分钟前
Linux 系统资源查看命令大全
linux·运维·服务器