首页 > 编程语言 > 详细

JAVA中的多线程(八):线程的优先级和yield方法

时间:2017-06-23 22:40:29      阅读:349      评论:0      收藏:0      [点我收藏+]

优先级代表着抢资源的频率

所有线程默认优先级是5

yield()临时释放线程的执行权

技术分享
 1 class Demo implements Runnable
 2 {
 3     public void run()
 4     {
 5         for(int x = 0; x < 70; x++)
 6         {
 7             System.out.println(Thread.currentThread().toString()+"..."+x);
 8             Thread.yield();
 9         }
10     }
11 }
12 
13 class JoinDemo
14 {
15     public static void main(String[] args) throws Exception
16     {
17         Demo d = new Demo();
18         Thread t1 = new Thread(d);
19         Thread t2 = new Thread(d);
20         t1.start();
21         //t1.join();
22         //t1.setPriority(Thread.MAX_PRIORITY);
23         t2.start();
24         for(int x = 0; x < 70; x++)
25         {
26             //System.out.println("main..."+x);
27         }
28         System.out.println("over");
29     }
30 }
View Code

 

JAVA中的多线程(八):线程的优先级和yield方法

原文:http://www.cnblogs.com/hepengke/p/7071789.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!