Java Threads: Thread scheduling-Concessions

Source: Internet
Author: User

Thread concessions using the Thread.yield () method, yield () is a static method that pauses the currently executing thread object and executes other threads.

/**
* Java线程:线程的调度-让步
*
* @author leizhimin
*/
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("线程1第" + i + "次执行!");
                 }
         }
}
class MyRunnable implements Runnable {
         public void run() {
                 for (int i = 0; i < 10; i++) {
                         System.out.println("线程2第" + i + "次执行!");
                         Thread.yield();
                 }
         }
}

线程2第0次执行!
线程2第1次执行!
线程2第2次执行!
线程2第3次执行!
线程1第0次执行!
线程1第1次执行!
线程1第2次执行!
线程1第3次执行!
线程1第4次执行!
线程1第5次执行!
线程1第6次执行!
线程1第7次执行!
线程1第8次执行!
线程1第9次执行!
线程2第4次执行!
线程2第5次执行!
线程2第6次执行!
线程2第7次执行!
线程2第8次执行!
线程2第9次执行!
Process finished with exit code 0

Source: http://lavasoft.blog.51cto.com/62575/221811

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.