/***
*
* @author 时光机jay
* @version 1.8
* @since /9/28
*
* */
public class Test33 {
static int a = 100;
public static void main(String[] args) {
// 内部类实现 Runnable 可以多个线程调用
Runnable th = new Runnable() {
@Override
public void run() {
while (true) {
// 同步代码块保证安全
synchronized(this) {
if (a <= 0) {
break;
}
System.out.println(Thread.currentThread().getName()+"卖第" + (a--) + "张票");
}
}
}
};
// 传入 Runnable对象创建Thread线程
Thread th1 = new Thread(th,"甲");
Thread th2 = new Thread(th,"乙");
Thread th3 = new Thread(th,"丙");
// 设置优先级
th1.setPriority(Thread.MAX_PRIORITY);
th2.setPriority(Thread.MIN_PRIORITY);
th3.setPriority(Thread.NORM_PRIORITY);
// 设置丙保护线程
th3.setDaemon(true);
// boolean daemon = th1.isDaemon();
// System.out.println(daemon);
th1.start();
th2.start();
th3.start();
}
}