admin管理员组

文章数量:1623801

代码:
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