4.java Art -java concurrent programming concurrent programming foundation

  From the beginning, the birth of java wise choice of built-in support for multi-threading, which makes the java language has obvious advantages compared to other languages ​​in the same period. As a minimum unit Caozuoxitong thread scheduling, a plurality of threads to execute simultaneously, which will significantly enhance the performance, a more evident in the multi-core environment. However, too many threads to create and mismanagement of the threads are likely to cause problems. This chapter focuses on the basics of java concurrent programming, from starting a thread to thread between different means of communication, the final chapter to the series described by a simple thread pool examples and applications (simple Web server).

1. Introduction thread

  1.1 What is the thread

    Modern operating systems when running a program that will create a process. For example, start a java program, the operating system will create a java process. Now the smallest unit is the operating system thread scheduling, also known as lightweight processes (Light Weight Process), you can create multiple threads in a process, these threads have property of the respective counter, stack and local variables, and can be accessed shared memory variables. High-speed switching processors on these threads, allowing users to feel these threads at the same time.

    A java program execution begins at main () method, and according to the established code logic execution, seemed to have no other threads involved, but in fact the java program is inherently multithreaded programs, because the implementation of main () method is a name for the main thread, using JMX below to see a common thread which contain the java program

public  static  void main (String [] args) {
         // the java thread management MXBean 
        the ThreadMXBean = ThreadMXBean the ManagementFactory.getThreadMXBean ();
         // do not need to acquire synchronization information of the monitor and synchronizer, threads and thread into the stack information acquisition 
        ThreadInfo [] threadInfos threadMXBean.dumpAllThreads = ( to false , to false );
         for (the ThreadInfo the ThreadInfo: threadInfos) { 
            . the System OUT .println ( " [ " + threadInfo.getThreadId () + " ] " + threadInfo.getThreadName ()); 
        } 

    } 
//    [. 7] the JDWP the Command Reader
 //     [. 6] the JDWP the Event Helper the Thread
 //     [. 5] The Listener the JDWP Transport: the dt_socket
 //     [. 4] Signal distribution processing thread is sent to the Dispatcher signal jvm
 //     [. 3] The method of the Finalizer call object finalize thread
 //     [2] Handler Reference clear reference thread
 //      [. 1] main main thread, the user program entry

    It can be seen not only face a run java program () method of operation, but the main thread executed simultaneously and several other threads.

  1.2 Why use multithreading

    Perform a simple "Hello word!", Has launched so many "do not care" thread, is not the simple issue complicated? Of course not, because true of the use of multi-threading, always able to bring significant benefits to developers using multi-threaded main reasons are the following:

      1. more processor cores

        As more and more the number of cores on the processor, as well as extensive use of Hyper-Threading technology, most computers now more than ever good at parallel computing, and ways to enhance processor performance, but also from a higher frequency to more multi-core development. How to use good core processor has become the main problem now

        A thread is the basic unit of most Caozuojitong scheduled a program to run as a process, the program is running to create multiple threads, one thread at a time can only run on a processor core. Imagine a single-threaded program is running only use one processor core, then no amount of processor core can not be added to significantly improve the efficiency of the implementation of the program. Conversely, if the change procedures using 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

        Something we will write some of the more complex code (This code is not to say that the complexity of complex algorithms, but complex business logic). For example, create an order, he includes insertion orders, generate orders snapshots, send e-mail to inform buyers and record sales of goods and so on. From the user clicking the "Order" button to start, we must wait to see these operations are completed successfully purchased products. But so many business operations, how can you let it complete with fast it?

        In the above scenario, you can use multi-threading technology, the upcoming data consistency is not strong operating distribute other threading (you can also use the message queue), such as generating a snapshot of the order, the benefits of doing so just want to send mail with a user request the thread can be processed as much as possible to shorten the response time, enhance the user experience.

      3. Better programming model

        java provides a good model for multi-threaded, elegant and consistent programming model, developers are able to focus more on solving the problem, the problem that is encountered in the establishment of an appropriate model, rather than their brains to consider how more than threading. Once developers have established a good model, slightly modified can always easily mapped to the java multithreaded programming model provided.

  1.3 thread priority

  1.4 thread state

  1.5 Daemon thread

2. Start and stop the thread

  2.1 construction thread

  2.2 start the thread

  2.3 understand interrupted

  2.4 expired suspend (), resume (), stop ()

  2.5 Termination security thread

3. inter-thread communication

  3.1 volatile and synchronized keyword

  3.2 Waiting / notification mechanism

  3.3 wait / notify classic case

  3.4 pipeline input / output streams

  3.5 Thread.jion()

  3.6 ThreadLocal use

4. threaded application examples

  4.1 Timeout waiting mode

  4.2 a simple exemplary database connection pool

  4.3-threading technology and its example

  4.4 thread pool based on a simple web server technology

5. Summary

 

Guess you like

Origin www.cnblogs.com/panda777/p/11355589.html