From:
Http://www.cnblogs.com/DreamSea/archive/2012/01/11/JavaThread.html
Overview of Threads (Introduction)
Thread definition (defining)
1) inheriting the Java.lang.Thread class
2) Implement Java.lang.Runnable interface
Start of thread (starting)
1) If the thread is inheriting the thread class
2) If the Runnable interface is implemented
Status of the thread (state)
New status (New)
Ready state (Runnable)
Operational status (Running)
Blocking state (Blocked)
Death Status (Dead)
Method of Thread, property
1) Priorities (priority)
2) Thread.Sleep ()/sleep (Long Millis)
3) Thread.yield ()
4) Thread.Join ()
5) object.wait ()
6) Object.notify ()/notifyall ()
7) Synchronizing Block
Overview of Threads (Introduction)
A thread is a program that executes multiple execution paths, executing a dispatch unit, relying on a process presence. The thread can not only share the memory of the process, but also have a memory space of its own, which is also called the line stacks, which is allocated by the system when the thread is established, and is used primarily to hold the data used internally by the thread, such as variables defined in the thread execution function.
Note: Multithreading in Java is a preemption mechanism rather than a time-sharing mechanism. Preemption refers to having multiple threads in a running state, but only one thread is allowed to run, and they preempt the CPU in a competitive way.
Thread definition (defining)
There are two ways to define a thread (defining a threads)
1) inheriting the Java.lang.Thread class
/** * Create a thread using the Inheritance java.lang.Thread class * * @author dreamsea 2011-12-29 20:17:06 */public class ThreadTest extends thread { /** * Override (Override) the run () method The JVM automatically calls the method */public void Run () { System.out.println ("I ' m running!"); }}
Note: The overridden (override) run () method is called by the JVM to automatically invoke the Run method after the start () method of the thread is invoked, but the overload run () method, like the normal member method, is not automatically run by the JVM by invoking the start () method of the thread. For example:
public class ThreadTest extends Thread { /** * Override (Override) run () method The JVM automatically calls the method */ @Override public void Run () { System.out.println ("I ' m running!"); } The/** * Overload (Overload) Run () method, like the normal method, does not run automatically by the JVM after the thread's start () method is called */public void run (int times) { System.out.println ("I ' m running! ( Overload) ");} }
It is not recommended to use this method to define a thread, because after you define a thread in the way that inherits thread, you cannot inherit other classes, resulting in a greatly reduced scalability of the program.
2) Implement Java.lang.Runnable interface
/** * Creates a thread by implementing the Runnable interface * @author Dreamsea */public class ThreadTest implements Runnable {public void run () {
system.out.println ("I ' m running!");} }
Start of thread (starting)
The premise of execution of any thread is that an instance of the thread class must exist, and the thread is started by calling the run () method.
1) If the thread inherits the thread class, the method is created as follows:
ThreadTest1 tt = new ThreadTest1 (); Tt.start ();
2) If you are implementing the Runnable interface, create the following method:
ThreadTest2 tt = new ThreadTest2 (); Thread t = new thread (TT); T.start ();
Status of the thread (state)
New State (new): When an instance of a thread is created using the new keyword and the thread class or its subclasses to create a threaded object, the thread is in the new state, and the new state of the thread has its own memory space, but the thread is not running. The thread is not alive at this time (not alive);
Ready state (Runnable): Initiates a thread to a ready state (Runnable) by invoking the start () method of the thread instance, and the thread in the ready state is already running, but is not yet assigned to the CPU and is not necessarily immediately executed, at the thread-ready queue. Wait for the system to allocate CPCU, wait state is not the execution state, at this time the thread is alive (alive);
Run state (Running): Once the CPU (by the JVM is selected), the thread goes into the running (Running) state, the thread's Run () method starts to execute, and the thread in the running state executes its operation in its own run () method until the other method is called to terminate, Or wait for a certain resource to block, or to complete the task of death; if there is no execution end within a given time slice, it will be replaced by the system to return to the thread's waiting state; the thread is Alive (alive);
Blocking state (Blocked): The thread is in a blocked (Blocked) state by calling join (), sleep (), wait (), or the resource is being taken up; the thread in blocking state is still alive (alive)
Dead State (Dead): When a thread's run () method is finished or is interrupted or exited abnormally, the thread reaches the dead (Dead) state. There may still be an instance object of that thread, and when the thready is already impossible to treat as a thread that can be executed independently, the thread's independent call stack has been dissolved. Once a thread enters the dead state, he can no longer enter the life cycle of a separate thread. For a thread in the dead state, the start () method is called, and an exception is run (runtime exception), and the thread in dead state is not alive (not alive).
Thread state Diagram (abbreviated)
Method of Thread, property
1) Priorities (priority)
Each class has its own priority, the general property is represented by an integer of 1-10, the default priority is 5, the priority is the highest is 10, the high priority thread is not necessarily higher than the priority of the thread, but the probability of execution is high; The priority of the default thread is the same as the thread that created it;
2) Thread.Sleep ()/sleep (Long Millis)
The time at which the current thread sleeps/millis (Millis Specifies that the sleep time is its minimum non-execution time, since sleep (Millis) is not guaranteed to be dispatched immediately by the JVM when Sleep arrives), and sleep () is a static method, So he won't stop the other threads are also dormant; the thread sleep () does not lose the owning object lock. Function: Keep the object lock, give up the CPU, call the purpose is not to let the current thread alone occupy the CPU resources obtained by the process, to leave a certain time for other threads to execute the opportunity;
3) Thread.yield ()
The yield () method will not work if there is no equal priority for the thread that gives the CPU the right to execute the opportunity for other threads to run the same priority thread (but does not guarantee that the current thread will be dispatched again by the JVM to bring the thread back into the running state).
4) Thread.Join ()
The thread that uses the method executes after the execution is complete.
5) object.wait ()
When a thread executes to the wait () method, he enters into a wait pool associated with the object (waiting pool) and loses the object's machine lock-temporarily, and returns the object lock after wait. The current thread must have a lock on the current object, and if the current thread is not the owner of the lock, the illegalmonitorstateexception exception is thrown, so wait () must be called in the synchronized block.
6) Object.notify ()/notifyall ()
Wakes the first thread/all threads waiting in the current object waiting pool. Notify ()/notifyall () must also have the same object lock, or a Illegalmonitorstateexception exception will be thrown.
7) Synchronizing Block
The Synchronized block/method Controls access to class member variables, and each object in Java has a unique, built-in lock.each synchronized block/method can only be accessed by holding a lock that calls the method's locked object, otherwise the owning thread is blocked, the machine lock is exclusive, and once it is held by a thread, the other thread cannot be owned (other synchronization methods cannot be accessed) , once the method executes, the lock is exclusive until the lock is released when the method returns, and then the blocked thread can obtain the lock and re-enter the executable state.
Java Thread Summary (GO)