In-depth discussion of Java thread
Author: Unknown
Article Source: Pioneer
1. What is thread generally, we call the procedures that are executing in your computer (Process) without calling it as a program. The "thread" is a single order of control flow in the process. Emerging operating systems, such as Mac, Windows NT, Windows 95, etc., mostly use multi-threaded concepts, regarding threads as basic execution units. The thread is also one of the considerable components in Java. Even the simplest applet is also done by multiple threads. In Java, any applet's Paint () and Update () methods are drawn by the AWT (Abstract Window Toolkit) plot and event processing threads, while the Applet is the main milestone method --init (), start (), STOP () and Destory () - are called by applying the applet. The concept of single threads is not new, really interesting is to use multiple threads while using multiple threads simultaneously in a program. Some parts are used in lightweig ht processing instead of threads, the similarity of threads and true processes is that they are all single sequential control flow. However, threads are considered to be lightweight because it runs in the context of the entire program, and can use resources and program environments throughout the program. As a single sequential control stream, threads in the running program must have some resources as the necessary overhead. For example, you must have a stack and program counter. The code executed within the thread acts only in its context, so some places use "execution context" instead of "thread". 2. Thread properties In order to use threads correctly and effectively, threads must understand all aspects of the thread and learn about the Java real-time system. You must know how to provide thread body, thread lifecycle, real-time system, how to schedule threads, thread groups, what is a ghost thread (Demo nthread). (1) All operations of the thread are in the thread body, and the line in Java is the RUN () method inherited from the Thread class, or the Run () method in the class of the runnable interface. When the thread is generated and initialized, its RUN () method is called in real time. The code within the run () method implements the behavior of the thread generated, which is the main part of the thread. (2) The thread state drawings represent the state that threads can be at any time in its life cycle, and the method of causing state change. This picture is not a complete finite state diagram, but basically summarizes more interested and universal aspects. The following discussion is based on the thread life cycle. ● New Thread generates a new thread to generate a Thread object. When the thread is in the "new thread" state, it is just an empty thread object, which has not been assigned to system resources. Therefore, you can only start or terminate it. Any other operations will trigger an exception. ● Runnable Start () method generates the resource, scheduling thread execution, and call the thread Run () method. At this time, the thread is running state. This state is not called a running state because the thread at this time is not always occupied. Especially for the PC with only one processor, there is only one thread occupancy process in which the running state. Java achieves sharing of multi-threads to process machines by scheduling. ● NOT Runnable When the following incident occurs, the thread enters the non-operating state. 1SUSPEND () method is called; 2Sleep () method is called; 3 thread uses Wait () to wait for condition variables; 4 threads are waiting for I / O. ● Death state (DEAD) When the Run () method returns, or other thread calls the stop () method, the thread enters the death state. Usually the Appl ET uses its STOP () method to terminate all threads it produce. (3) Things priority though we say that the thread is running concurrently. However, the fact is often not the case. As mentioned earlier, when there is only one CPU in the system, the multi-thread is executed in a single CPU in some order, called Schedu Ling. Java uses a simple, fixed scheduling, ie fixed priority scheduling. This algorithm is implemented based on the relative priority of the running state thread. When the thread is generated, it inherits the priority of the original thread. The priority can be modified when needed.
At any time, if there are multiple threads waiting to run, the system selects the highest priority running thread run. Only when it stops, automatically abandon, or because some reason is a low priority thread to run. If the two threads have the same priority, they will run alternately. The thread scheduling algorithm for the Java real-time system is still mandatory, at any time, if a state of a thread that is high than the other thread priority becomes a running state, the real-time system will select the thread to run. (4) Narciting threads can become a ghost thread. It is a service provider as an object and thread running within the same process. For example, the Hotjava browser has a ghost thread called "Background Photo Reader", which reads images from file systems or networks for objects and threads that need images. The ghost thread is a typical independent thread in the application. It serves other objects and threads in the same application. The RUN () method of the ghost thread is generally infinite loop, waiting for service request. (5) Thread groups each Java thread is a member of a thread group. The thread group provides a mechanism that allows multiple threads to be integrally operated in one object. For example, you can use a method to call all threads within the group. The Java thread group is implemented by the ThreadGroup class. When thread is generated, you can specify a thread group or put it in a default thread group by a real-time system. The thread can only belong to a thread group, and the thread group to which it belongs cannot be changed after the thread generation. 3. Multi-threaded procedures are not much to say for multithreaded benefits. However, it also brings some new trouble. As long as you pay attention to the design procedure, you should not be too difficult to overcome these troubles. (1) Synchronous thread Many threads must consider sharing data or coordinated execution between other threads in execution. This requires a synchronization mechanism. Each object in Java has a lock corresponding. But Java does not provide separate LO CKs and UNLOCK operations. It is implicitly implemented by the high-level structure to ensure the corresponding operation. (However, we note that the Java virtual machine provides a separate Monito Renter and MonitorExit instructions to implement the Lock and UNLO CK operations.) SYNCHRONIZED statement calculates an object reference, trying to complete the lock operation on the object, and stop processing before the lock operation . When the lock operation is completed, the SYNCHRONIZED statement is performed. When the statement is performed (no matter normal or abnormal), the unlock operation is automatically completed. As an object-oriented language, Synchronized is often connected to methods. A better way is that if a variable is assigned by a thread and is referenced or assigned by another thread, all access to this variable must be in a synchromized statement or the SYNCH RONIZED method. Now, a situation is now: Thread 1 and thread 2 have access to a data area, and ask thread 1 to access the thread 2, then only synchronized is not solving the problem. This can be implemented in UNIX or Windows NT. Java is not available. Provided in Java is WAIT () and Noti fy () mechanisms. Use the following: Synchronized method-1 (...) {call by thread 1. ∥ Access data area; available ∥ Access = true; notify ()} synchronized method-2 (...) {∥call by thread 2. While (! Available) Try { Wait (); ∥WAIT for notify ().} Catch (Interrupted Exception E) {} ∥ Access data area} where available is a class member variable, set the initial value of false. If you check the available in Method-2 is false, call Wait (). The function of Wait () is to make the thread 2 into the non-operating state and unlock. In this case, METHOD-1 can be called by thread 1. When notify () is performed. Thread 2 is converted from non-running state to a runoff state. When the Method-1 call returns. Thread 2 can be restored to the object, and the instruction after the latch is successfully executed. This mechanism can also apply to other more complex situations.