Go Goroutine and Java multithreading comparison

Source: Internet
Author: User

Introduction: Personal understanding of threads, co-processes and single, multicore threads

1. Single-core CPU running multithreaded programs, the same time only one thread running, the system to help you switch threads (CPU time slicing), the system to each thread allocated time slices to execute, each time slice about 10ms, looks like the same run, But actually each thread runs a little bit to the other threads continue to run, efficiency will not improve, switching threads will instead increase the overhead (thread context switch), macro can look at the parallel, the single core is only concurrency, a real CPU core executes only at the same time a thread (not a process).
2. The use of multithreading is to do a time-consuming operation, it is necessary to wait for the return results, when using multithreading can increase the degree of program concurrency. Multithreading is wasteful if a task that does not require any waiting and sequential execution can be done.
3. Personal insights, for threads runable and Threadpoolexcutor, the thread pool is built using single-core execution (pseudo-parallel concurrent multithreading), while the Fork/join concurrency framework provided in jdk1.7 The use of multi-core task segmentation execution, the personal feel and map/reduce have certain similarities.
4. The process is a user-state lightweight thread, the scheduling of the association is entirely user-controlled. While the thread scheduling is the operating system kernel control, through the user's own control, can reduce the context of frequent switching system overhead, improve efficiency.

Environment:
    1. Ubuntu 16.04 LTS
Testing process
对比程序,系统发生1千万次并发,并发为一个无操作的空函数,使用time指令对比性能

Java full version

ImportJava.util.concurrent.ExecutorService;ImportJava.util.concurrent.Executors;/** * Created by Zhoudazhuang on 14-8-15. * jdk1.7 and a bit * new Runnable () {@Overridepublic void Run () {}jdk1.8 can be Use lambda (), {} * 1. Javac Main.java * 2. Java Main (Java binary bytecode so that it can be run but it is ready to be packaged to run the jar package) * 3.  JAR-CVF My.jar *.class * 4. Time Java-server-jar My.jar My.jar does not have the main manifest property modified in the compressed Jar MANIFEST.MF add Main-class: To empty a grid main (main class name, with package or the second method is specified directly in the parameter) * 5. Use time Java-server-jar My.jar or directly using time JAVA-CP when you are finished./my.jar Main//Do not use-jar the-CP directory and the Zip/jar file of the class search path directly specified can be * output *  # time Java-server-jar My.jar * Elapsed time:0.041s * Java-server-jar my.jar 0.26s user 0.01s system 215% CPU 0.126 Total * Time LS; Time Java is a Linux command without-server I feel the output is the same * / Public  class Main {    Private Static Final intTimes = -* +* -; Public Static void Main(string[] args)throwsException {Executorservice service = Executors.newfixedthreadpool (Runtime.getruntime (). Availableprocessors ());LongT1 = System.currenttimemillis (); for(intI=0; i<times;i++) {Service.submit (NewRunnable () {@Override                 Public void Run() {                }            }); } service.shutdown ();LongT2 = System.currenttimemillis (); System.out.printf ("Elapsed time:%.3fs\n", (T2-T1)/ +f); }}

Output

# time java -cp ./my.jar Maintime14.589sjava -cp ./my.jar Main  75.20s2.96ssystem48616.072 total

Golang full version

Contrast program, system occurs1Billions of times concurrent, concurrency is an empty function with no operation, using the time command to compare the performance of the package Mainimport ("Runtime"    "FMT"    "Time") Const (Times = -* +* -) Func main () {runtime. Gomaxprocs(Runtime. Numcpu()) FMT. Println("CPUs:", runtime. Numcpu(),"Goroutines:", runtime. Numgoroutine()) T1: = time. now() for i:=0; i<times; i++ {Go func () {} ()} for Runtime. Numgoroutine() >4{//FMT. Println("Current goroutines:", runtime. Numgoroutine())//time. Sleep(Time. Second)} t2: = Time. now() FMT. Printf("Elapsed time:%.3fs\n", T2. Sub(t1). Seconds())}

Execution Result:

81time3.582s./compareJava  13.091.49system4053.591 total

Due to the different processing mechanism of multithreading, Java processing time, CPU load and so on obviously high and goroutine, so in this case, goroutine in the concurrent multitasking ability has the inherent advantage.

Postscript

Example programs are mainly referenced from: https://my.oschina.net/u/209016/blog/301705

Go Goroutine and Java multithreading comparison

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.