Crazy Java Learning Note (-----------thread advanced)

Source: Internet
Author: User

Status of each phase of the Java thread:

So let's start with a little research:

Start thread

One, define the thread 1, extended Java.lang.Thread class.  There is a run () method in this class, and you should be aware of its usage:
Run ()
If the thread is constructed using a stand-alone Runnable running object, the method of the object is called, Runnable run Otherwise the method does nothing and returns. subclasses should override this method.
Thread
2, realize java.lang.Runnable interface.  void Run()
Runnable When you create a thread with an object that implements an interface, starting the thread causes the method of the object to be called in a thread that executes independently run . the general contract for a method is that it may perform any required operations.
run
  second, instantiate the thread  1, if the extension of the Java.lang.Thread class thread, then the direct new can.  2, if the implementation of the Java.lang.Runnable interface class, then use the thread construction method:Thread (Runnable target)
Thread (Runnable target, String name)
Thread (threadgroup group, Runnable target)
Thread (threadgroup group, Runnable Target, String name)
Thread (threadgroup group, Runnable Target, String name, long stackSize)  third, start the thread call the Start () method on the thread object of the thread, not run () or any other method.  before the start () method is called: The thread is in the new state, the new state refers to a thread object, but there is no real thread.  after calling the start () method: A series of complex things happenedstart a new execution thread (with a new call stack);the thread is transferred from the new state to the operational state;When the thread obtains an opportunity to execute, its target run () method runs.  Note: For Java, the run () method has nothing special. Like the main () method, it is just a new thread that knows the method name (and signature) of the call. Therefore, it is legal to raise the Run method on runnable or thread. But does not start a new thread.   third, start the thread   Call the start () method on the thread object of the thread, not run () or any other method.  before the start () method is called: The thread is in the new state, the new state refers to a thread object, but there is no real thread.  after calling the start () method: A series of complex things happenedstart a new execution thread (with a new call stack);the thread is transferred from the new state to the operational state;When the thread obtains an opportunity to execute, its target run () method runs. (If you don't understand, take a closer look!) ) Note: For Java, the run () method has nothing special. Like the main () method, it is just a new thread that knows the method name (and signature) of the call. Therefore, it is legal to raise the Run method on runnable or thread. But does not start a new thread. Example:implementing threads by inheriting the thread class
Package com.haixu.thread;/* * Thread Practice * Create thread classes by inheriting the threads class * */public class Firstthread extends thread{private int i;//rewrite run () method, the anti-bounce of the Run () method is the thread's execution body public void run () {for (; I < 5; i++) {//) when the thread class inherits the thread class, use this directly to get the current thread// The thread object's GetName () returns the name of the current thread//So you can call the GetName () method directly to return the name of the current thread System.out.println (getName () + "  " + i);}}  public static void Main (string[] args) {for (int i = 0; i<4; i++) {System.out.println (Thread.CurrentThread (). GetName () + "  " + i), if (i = = 3) {//Create first thread new Firstthread (). Start ();//Create a second thread new Firstthread (). Start ();}}}

Operation Result:
Main  0main  1main  2main  3thread-0  0thread-0  1thread-0  2thread-0  3thread-0  4thread-1  0thread-1  1thread-1  2thread-1  3thread-1  4

To create a threading class by implementing the Runnable interface
Package com.haixu.thread;/* * Thread Learning * Create thread classes by implementing the Runnable interface * */public class Secondthread implements runnable{private int i; The run () method is also the execution body of the thread, public void run () {for (; i<5; i++) {/////When the thread class implements the Runnable interface//If you want to get the current thread, Only by calling the Threand.currentthread () method System.out.println (Thread.CurrentThread (). GetName () + "  " + i);}}  public static void Main (string[] args) {for (int i = 0; i<4; i++) {System.out.println (Thread.CurrentThread (). GetName () + "  " + i), if (i==3) {Secondthread st = new Secondthread ();//Create New Thread (    St, "thread 1") through the newly-thread (Target,name) method . Start ();    New Thread (ST, "Thread 2"). Start ();}}}    

Operation Result:
Main  0main  1main  2main  3 thread 1  0 thread 1  1 thread 2  1 thread 1  2 thread 2  3 thread 1  4

four or one some FAQs1, the name of the thread, a running thread always has a name, the name has two sources, one is the name of the virtual machine itself, one is your own fixed name. In the absence of a specified thread name, the virtual machine always assigns a name to the thread, and the main thread's name is always Mian, and the name of the non-main thread is indeterminate. 2, the thread can be set name, can also get the name of the thread, even the main course is no exception. 3. The method to get the object of the current thread is: Thread.CurrentThread (); This method is a static method of the thread class. 4, in the above code, can only guarantee: Each thread will start, each thread will run until completion. A series of threads starting in some order does not mean that they will be executed in that order. For any set of startup threads, the scheduler does not guarantee its execution order and the duration is not guaranteed. 5. When the thread target run () method ends, the thread finishes. 6. Once the thread is started, it can never be restarted again. Only one new thread can be started, and only once. A running thread or a dead thread can be restarted. 7, thread scheduling is a part of the JVM, on a CPU machine, in fact, can only run one thread at a time. Only one thread stack executes at a time. The JVM thread scheduler determines which threads are actually running in a running state. One of the many running threads can be selected as the current thread. The order in which the running threads are selected to run is not guaranteed. 8, although the queue form is usually used, but this is not guaranteed. The queue form is when a thread completes a "round", it moves to the tail of the running queue until it finally queues up to the front end of the queue before it can be selected again. In fact, we call it a running pool instead of a running queue, to help realize that threads are not all in a guaranteed order to sing a queue. 9, although we do not have the ability to control the thread scheduler, but can be other ways to affect the way thread scheduling. transition of thread state State transitions for thread state threads are the basis of thread control. The total thread state can be divided into five states: Raw, dead, can run, run, wait/block. For example: 1, new state: The thread object has been created, and the start () method has not been invoked on it. 2. Operational status: When the thread is eligible to run, but the scheduler has not selected it as the state in which the thread is running. When the start () method is called, the thread first enters the operational state. After the thread has run or has returned from blocking, waiting, or sleeping, it returns to the operational state. 3. Running state: The thread Scheduler selects a thread from a running pool as the current thread is in the state of the thread. This is also the only way that a thread goes into a running state. 4. Wait/block/Sleep state: This is the state at which the thread is eligible to run. In fact, this three-state combination is one, and its common denominator is that the thread is still alive, but there is no condition to run at the moment. In other words, it is operational, but if an event occurs, he may return to the operational state. 5. Death state: When the thread's run () method finishes, it is considered dead. This thread object may be alive, but it is not a separate thread. Once a thread dies, it cannot be resurrected. If you call the start () method on a dead thread, the java.lang.IllegalThreadStateException exception is thrown. Specific implementation process see the next blog post for examples of each state

Crazy Java Learning Note (-----------thread advanced)

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.