Java thread collaboration yield (), java thread collaboration yield

Source: Internet
Author: User

Java thread collaboration yield (), java thread collaboration yield

 

Yield (): method definition

Calling the yield method will allow the current thread to hand over the CPU permission and allow the CPU to execute other threads. However, yield cannot control the time when the CPU is handed over. In addition, the yield method can only give threads with the same priority the opportunity to get the CPU execution time (Yield tells the currently executing thread to give the running opportunity to the thread pool with the same priority, the cpu will select from a large number of executable states .), line businesses with higher priorities may not necessarily obtain the execution right. They only have a higher probability. This method does not release the lock.
Note: calling the yield method does not cause the thread to enter the blocking state, but enables the thread to return to the ready state. It only needs to wait for a new CPU execution time (that is, the current thread, which is just the one, may be executed again,
It does not mean that other threads will be executed, but this thread will not be executed in the next time ).

Example: Hand over the execution right of the current cpu and re-obtain the cpu scheduling with other threads
Public class ThreadYieldTest {static Object object = new Object (); public static void main (String [] args) {Thread thread_a = new Thread (new Thread_a ()); thread thread_ B = new Thread (new Thread_ B (); thread_a.setPriority (Thread. MIN_PRIORITY); // Min Priority thread_ B .setPriority (Thread. MAX_PRIORITY); // Max Priority thread_a.start (); thread_ B .start () ;}} class Thread_a implements Runnable {@ Override public void run () {try {for (int I = 0; I <5; I ++) {// when I is 3, this thread will let the CPU time off, let others or their own threads execute (that is, who is the first to get the first to execute) if (I = 3) {Thread. yield ();} System. out. println (Thread. currentThread (). getName () + "execution =" "+ I) ;}} catch (Exception e) {e. printStackTrace () ;}} class Thread_ B implements Runnable {@ Override public void run () {try {for (int I = 0; I <5; I ++) {if (I = 3) {Thread. yield ();} System. out. println (Thread. currentThread (). getName () + "execution =" "+ I) ;}} catch (Exception e) {e. printStackTrace ();}}}

Running result:

First case: When Thread-0 is executed to 3, the CPU time will be dropped. At this time, Thread-1 will grab the CPU time and execute it.

Case 2: When Thread-0 is executed to 3, the CPU time will be dropped. At this time, Thread-0 will grab the CPU time and execute it.

Example: It is similar to the sleep method and does not release the lock.

Public class ThreadYieldTest {static Object object = new Object (); public static void main (String [] args) {Thread thread_a = new Thread (new Thread_a ()); thread thread_ B = new Thread (new Thread_ B (); thread_a.setPriority (Thread. MIN_PRIORITY); // Min Priority thread_ B .setPriority (Thread. MAX_PRIORITY); // Max Priority thread_a.start (); thread_ B .start () ;}} class Thread_a implements Runnable {@ Override public void run () {try {synchronized (ThreadYieldTest. object) {System. out. println ("Enter Thread" + Thread. currentThread (). getName (); for (int I = 0; I <5; I ++) {// when I is 3, the thread will throw the CPU time, let others or their own threads execute (that is, who is the first to get the first to execute) if (I = 3) {Thread. yield ();} System. out. println (Thread. currentThread (). getName () + "execution =" "+ I) ;}} catch (Exception e) {e. printStackTrace () ;}} class Thread_ B implements Runnable {@ Override public void run () {try {synchronized (ThreadYieldTest. object) {System. out. println ("Enter Thread" + Thread. currentThread (). getName (); for (int I = 0; I <5; I ++) {if (I = 3) {Thread. yield ();} System. out. println (Thread. currentThread (). getName () + "execution =" "+ I) ;}} catch (Exception e) {e. printStackTrace ();}}}

Execution Result: No matter how the execution is repeated, one thread can only be executed after the execution of another thread is complete.

 

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.