Why use multithreading?

Java programs are inherently multithreaded program, execute a Java program in the main () method is actually running at the same time the main thread and a number of other threads. As shown in the following:

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

public class readExcel {
    public static void main(String[] args) {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
        for (ThreadInfo threadInfo :threadInfos){
            System.out.println("[" + threadInfo.getThreadId() + "]" + threadInfo.getThreadName());
        }
    }
}
Output is as follows: 
[ . 6] Monitor Ctrl- Break 
[ . 5 ] The Listener the Attach 
[ . 4 ] Signal processing is sent to the Dispatcher // distributing threads in the JVM signal 
[ . 3 ] // call the Finalizer thread finalize method of        
[ 2 ] // Handler Reference Clear Reference thread 
[ 1] main // main thread, the user program entry

 

Why use multithreading?

(1) more processor cores

A single-threaded program can use only one processor core is running, then no amount of processor core can not be added to the remarkable efficiency of the implementation of the program. Conversely, if the program to use multithreading, the calculation logic assigned to a plurality of processor cores, will significantly reduce the processing time of the program, and as more processors are added and the core becomes more efficient.

(2) faster response time

You can use multi-threading technology, data consistency is not strong operating distributed to other threads (also can use the message queue ). The benefit of this is in response to a user request processing threads can be done as quickly as possible to shorten the response time, enhance the user experience.

For example: a user submits an order, a series of follow-up operation will wait to see the results of all completed subscription success. For some of these business operations, consider other threads to handle, making it done faster.

(3) better programming model

Java multi-threaded programming provides a good, elegant and consistent programming model, when developers create a suitable model for the problems encountered, slight modifications can be easily mapped onto multi-threaded programming model for Java provides.

Guess you like

Origin www.cnblogs.com/Aug-20/p/11808138.html