admin管理员组文章数量:1623793
代码:
package cn.tedu.thread;
public class PriorityDemo {
public static void main(String[] args) {
//创建线程对象
Thread t1=new Thread(new PDemo(),“A”);
Thread t2=new Thread(new PDemo(),“B”);
//设置优先级
t1.setPriority(1);
t2.setPriority(10);
//开启线程
t1.start();
t2.start();
}
}
class PDemo implements Runnable{
@Override
public void run() {
for(int i=0;i<=10;i++) {
System.out.println(Thread.currentThread().getName() + “,” + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
输出:
B,0
A,0
B,1
A,1
B,2
A,2
B,3
A,3
B,4
A,4
B,5
B,6
A,5
B,7
A,6
B,8
A,7
B,9
A,8
B,10
A,9
A,10
代码图:
输出:
本文标签: 优先级线程setPriority
版权声明:本文标题:线程优先级-------setPriority( )设置优先级 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728897470a1178597.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论