Java thread (9): Thread Scheduling-concession

Source: Internet
Author: User

Java thread: Thread Scheduling-concession the concession meaning of the thread is to let the currently running thread out of CPU resources, but who does not know, just let out, the thread status back to the runable status. Thread concession uses the Thread. yield () method, and yield () is a static method. The function is to pause the currently executed Thread object and execute other threads. /*** Java thread: Thread Scheduling-concession ** @ author leizhimin 9:02:40 */public class Test {public static void main (String [] args) {Thread t1 = new MyThread1 (); Thread t2 = new Thread (new MyRunnable (); t2.start (); t1.start ();}} class MyThread1 extends Thread {public void run () {for (int I = 0; I <10; I ++) {System. out. println ("thread 1" + I + "times! ") ;}} Class MyRunnable implements Runnable {public void run () {for (int I = 0; I <10; I ++) {System. out. println ("thread 2" + I + "times! "); Thread. yield () ;}} Thread 2 0th executions! Thread 2, 1st executions! Thread 2, 2nd executions! Thread 2, 3rd executions! Thread 1 executes 0th times! Thread 1 executes 1st times! Thread 1 executes 2nd times! Thread 1 executes 3rd Times! Thread 1 executes 4th times! Thread 1 executes 5th times! Thread 1 executes 6th times! Thread 1 executes 7th times! Thread 1 executes 8th times! Thread 1 executes 9th times! Thread 2, 4th executions! Thread 2, 5th executions! Thread 2, 6th executions! Thread 2, 7th executions! Thread 2, 8th executions! Thread 2, 9th executions!

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.