线程优先级和名字的使用

package charpter05;

//实现接口
public class Processor implements Runnable {
// Processor类重写接口的方法
@Override
public void run() {
// 遍历线程执行结果
for (int i = 0; i <= 100; i++) {
// 线程调用当前线程的方法和调用名字
System.out.println(Thread.currentThread().getName() + "******" + i);

}
try {
// 线程启动sleep方法,抛出异常,捕捉异常
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

------------------------------

package charpter05;

public class TestSleep {

public static void main(String[] args) {
// 创建对象
Processor p = new Processor();
// 创建线程
Thread t1 = new Thread(p);
Thread t2 = new Thread(p);
// 启动线程
t1.start();
t2.start();
// 设置名字
t1.setName("AAAAAAAAAAAAAAA");
t2.setName("MMMMMMMMMMMM");
// 设置前后顺序
t1.setPriority(1);
t2.setPriority(10);// 优先级大的先执行

}

}

猜你喜欢

转载自www.cnblogs.com/Koma-vv/p/9617729.html