日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

go和java线程,Go的多线程和pthread或Java线程有什么区别?

發布時間:2025/3/20 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 go和java线程,Go的多线程和pthread或Java线程有什么区别? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

正如之前的答案所述,go例程并不一定與系統線程相對應,但是如果你現在必須提高多線程的性能,我發現以下內容很有用:

The current implementation of the Go runtime will not parallelize this code by default. It dedicates only a single core to user-level processing. An arbitrary number of goroutines can be blocked in system calls, but by default only one can be executing user-level code at any time. It should be smarter and one day it will be smarter, but until it is if you want CPU parallelism you must tell the run-time how many goroutines you want executing code simultaneously. There are two related ways to do this. Either run your job with environment variable GOMAXPROCS set to the number of cores to use or import the runtime package and call runtime.GOMAXPROCS(NCPU). A helpful value might be runtime.NumCPU(), which reports the number of logical CPUs on the local machine. Again, this requirement is expected to be retired as the scheduling and run-time improve.

quote source

最大化我的i5處理器的一個示例程序就是這個(在htop中使用100%的所有4個核心):

package main

import (

"fmt"

"time"

"runtime"

)

func main() {

runtime.GOMAXPROCS(4) // Set the maximum number of threads/processes

d := make(chan string)

go boring("boring!", d, 1)

go boring("boring!", d, 2)

go boring("boring!", d, 3)

go boring("boring!", d, 4)

for i := 0; i < 10; i++ {

time.Sleep(time.Second);

}

fmt.Println("You're boring; I'm leaving.")

}

func boring(msg string, c chan string, id int) {

for i := 0; ; i++ {

}

}現在它實際上并沒有“做”任何事情,但是看看與其他語言(如Java)編寫多線程應用程序相比有多簡短/簡單/簡單。

總結

以上是生活随笔為你收集整理的go和java线程,Go的多线程和pthread或Java线程有什么区别?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。