码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA~多线程:sleep、yield方法

时间:2016-04-01 14:35:00      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

sleep不考虑其它线程的优先级

yield让位给相同或更高优先级的线程

sleep

技术分享

yield

技术分享

package multiThread2;

public class TestThread042Yield {
    public static void main(String[] args) {
        MyThread3 t1 = new MyThread3("t1");
        MyThread3 t2 = new MyThread3("t2");
        MyThread3 t3 = new MyThread3("t3");
        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.NORM_PRIORITY);
        t3.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();
        t3.start();
    }
}

class MyThread3 extends Thread {
    MyThread3(String s) {
        super(s);
    }
    public void run() {
        for (int i = 1; i <= 100; i++) {
            System.out.print("");
            if (i % 10 == 0) {
//                try {
//                    sleep(10);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                yield();
                System.out.println(this.getName() + ":"+ i);
            }
        }
    }
}

 

JAVA~多线程:sleep、yield方法

标签:

原文地址:http://www.cnblogs.com/AndyHoo/p/5344661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!