Java Basics Review Java Thread class learning (10)--Introduction to the state of threads and the methods used for conversion

Source: Internet
Author: User

Overview of Threads:

A thread is a program's multiple execution path, the unit that executes the dispatch, relying on the process existence. The thread can not only share the memory of the process, but also have a memory space of its own, which is called the line stacks, which is allocated by the system when the thread is established, and is mainly used to hold the data inside the thread, such as the variables defined in the thread execution function.

J Multithreading in Ava is a preemption mechanism rather than a time-sharing mechanism. The preemption mechanism is that the CPU resource is shared by multiple threads, multiple threads are in a running state, but only one thread is allowed to run, and they preempt the CPU in a competitive way. You can refer to the differences between Java processes and threads

The state of the thread:

  • New State (new): When a thread is created on a thread instance, new thread () or new thread (Runnable r), this thread is in a new state, in a new state, the thread has its own memory space, but the thread is not running, At this point the thread is not alive (not Alive).
  • ready State (Runnable): The Thread object's start () method starts the thread into a ready state (Runnable), the thread in the ready state has a running condition (CPU execution qualification), but has not yet grabbed the CPU execution, Will not necessarily be executed immediately. At this point the thread is ready in the queue, waiting for the system to allocate its CPU. The wait state is not the execution state, at which point the thread is Alive (isalive=true).
  • running State (Running): Once the thread gets to the CPU execution, the thread goes into the running state (Running), the thread run method starts executing, and the running state thread executes its own code inside the Run method until the other method is called and terminates. Or wait for a certain resource to block, or to complete the task of death, if the given time on-chip program does not end, it will be suspended by the system currently running scheduler, back to the waiting state of the thread. The thread is alive at this time (Alive)
  • Blocking state: (Blocked): The thread in the blocking state is still alive by calling Join,sleep,wait or the resource is occupied, causing the thread to be in a blocking state (Blocked).
  • Death Status: (Dead): When the code in the Run method of a thread finishes running, or is interrupted, or is exited abnormally, the thread is in a dead state. Once the thread enters the dead state, it can no longer enter the life cycle of a separate thread, and a runtime exception exception occurs when a thread in the dead state calls the Start method, and the thread in the dead state is not alive (Alive)

Methods and properties of the thread:

  • priority: Each thread has a priority. the default priority is 5, the priority is the highest is 10, the 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 him; SetPriority (thread.max_ Priority) method to set the thread precedence.
  • sleep (Long Millis)/sleep (long millis, int nanos) thread Hibernation : Causes the thread to hibernate when it is executed for a specified time. 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;
  • Thread.yield (): Give up CP u execution, pause the currently executing thread object, and let the same priority thread run . A little release of execution for a while, the time will not be very long.
  • Thread.Join (): used to temporarily join thread execution , Rob CPU execution right . For example, when a thread executes a join method to a B thread, a waits, and a thread executes after the B thread finishes executing.
  • object.wait (): used in synchronization, throws an Illegalmonitorstateexception exception if not used in the lock. Causes the thread to be in a blocking state, the thread enters the waiting pool, releases the lock , releases the execution

  • 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.
  • Synchronizing Block: The machine lock is exclusive, once it is held by a thread, the other thread can no longer have (no access to other synchronization methods), once the method executes, the lock is exclusive until the lock is released from the method, and then the blocked thread can obtain the lock and re-enter the executable state.

The test code for the above method is given below:

yield (),join(), The priority attribute of the test, other methods are not tested, in my article can be seen:

Design two threads, one thread for the main thread, and one thread Thread-0:

Code: There is no alternating execution of the main thread and Thread-0 with the Join method. As we all know, it's not tested here. But with the Join method we look at the effect.

 PackageCom.lp.ecjtu.Thread;/** *  * @authorAdministrator * When a thread executes to the join method of the B thread, a waits, and the a thread executes after the B thread executes the * Join method can be used to temporarily join the thread execution*/classJoinImplementsrunnable{@Override Public voidrun () {//Thread.yield ();//pauses the first thread object that is executing and executes other objects. A little release execution right a moment         for(inti=0;i<20;i++){            //The tostring method of the replication parent class that returns the string representation of the thread, including the thread name, priority, and thread group. //Thread[thread-1,5,main] The default priority is 5, and the priority is from 1-10System.out.println (Thread.CurrentThread (). toString () + "-" +i); }    }    } Public classJoindemo {/**     * @paramargs *@throwsinterruptedexception*/     Public Static voidMain (string[] args)throwsinterruptedexception {Join Join=NewJoin (); Thread T1=NewThread (join); Thread T2=NewThread (join); //t1.setpriority (thread.max_priority);//methods for setting the priority of a threadT1.start (); T2.join ();//At some point the Join method allows T2 to temporarily join the thread, robbing the main thread of the CPU right, until T2 executes, T1 executesT2.start (); }}

Output Result:

thread[thread-0,5,main]-0
thread[thread-1,5,main]-0
thread[thread-1,5,main]-1
thread[thread-1,5,main]-2
thread[thread-1,5,main]-3
thread[thread-1,5,main]-4
thread[thread-1,5,main]-5
thread[thread-1,5,main]-6
thread[thread-1,5,main]-7
thread[thread-1,5,main]-8
thread[thread-1,5,main]-9
thread[thread-1,5,main]-10
thread[thread-1,5,main]-11
thread[thread-1,5,main]-12
thread[thread-1,5,main]-13
thread[thread-1,5,main]-14
thread[thread-1,5,main]-15
thread[thread-1,5,main]-16
THREAD[THREAD-1,5,MAIN]-17
thread[thread-1,5,main]-18
thread[thread-1,5,main]-19
Thread[thread-0,5,main]-1
Thread[thread-0,5,main]-2
Thread[thread-0,5,main]-3
Thread[thread-0,5,main]-4
Thread[thread-0,5,main]-5
Thread[thread-0,5,main]-6
Thread[thread-0,5,main]-7
Thread[thread-0,5,main]-8
Thread[thread-0,5,main]-9
Thread[thread-0,5,main]-10
Thread[thread-0,5,main]-11
Thread[thread-0,5,main]-12
Thread[thread-0,5,main]-13
Thread[thread-0,5,main]-14
Thread[thread-0,5,main]-15
Thread[thread-0,5,main]-16
Thread[thread-0,5,main]-17
Thread[thread-0,5,main]-18
Thread[thread-0,5,main]-19


Can see the blue part, a moment T2 through the join method strong to execution, after execution, Thread-0 execution.

Example 2, Priority testing

 PackageCom.lp.ecjtu.Thread;/** *  * @authorAdministrator * When a thread executes to the join method of the B thread, a waits, and the a thread executes after the B thread executes the * Join method can be used to temporarily join the thread execution*/classJoinImplementsrunnable{@Override Public voidrun () {         for(inti=0;i<20;i++){            //The tostring method of the replication parent class that returns the string representation of the thread, including the thread name, priority, and thread group. //Thread[thread-1,5,main] The default priority is 5, and the priority is from 1-10System.out.println (Thread.CurrentThread (). toString () + "-" +i); }    }    } Public classJoindemo {/**     * @paramargs *@throwsinterruptedexception*/     Public Static voidMain (string[] args)throwsinterruptedexception {Join Join=NewJoin (); Thread T1=NewThread (join); Thread T2=NewThread (join); T2.setpriority (thread.max_priority);//methods for setting the priority of a threadT1.start ();T2.start ();     }}

Output Result:

thread[thread-1,10,main]-0
thread[thread-1,10,main]-1
thread[thread-1,10,main]-2
thread[thread-1,10,main]-3
thread[thread-1,10,main]-4
thread[thread-1,10,main]-5
thread[thread-1,10,main]-6
thread[thread-0,5,main]-0
Thread[thread-1,10,main]-7
Thread[thread-1,10,main]-8
Thread[thread-0,5,main]-1
Thread[thread-1,10,main]-9
Thread[thread-1,10,main]-10
Thread[thread-1,10,main]-11
Thread[thread-1,10,main]-12
Thread[thread-1,10,main]-13
Thread[thread-1,10,main]-14
Thread[thread-1,10,main]-15
Thread[thread-1,10,main]-16
Thread[thread-1,10,main]-17
Thread[thread-1,10,main]-18
Thread[thread-0,5,main]-2
Thread[thread-1,10,main]-19
Thread[thread-0,5,main]-3
Thread[thread-0,5,main]-4
Thread[thread-0,5,main]-5
Thread[thread-0,5,main]-6
Thread[thread-0,5,main]-7
Thread[thread-0,5,main]-8
Thread[thread-0,5,main]-9
Thread[thread-0,5,main]-10
Thread[thread-0,5,main]-11
Thread[thread-0,5,main]-12
Thread[thread-0,5,main]-13
Thread[thread-0,5,main]-14
Thread[thread-0,5,main]-15
Thread[thread-0,5,main]-16
Thread[thread-0,5,main]-17
Thread[thread-0,5,main]-18
Thread[thread-0,5,main]-19

Summary: From the results can be seenThread-1,10,mainThread 2 with priority 10 executes first.

The yield method is the execution of the thread that is executing the execution right, pausing for a short while. The accident here ...

Java Basics Review Java Thread class learning (10)--Introduction to the state of threads and the methods used for conversion

Related Article

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.