//優先級
public class Priority {
public static void main(String[] args) {
MyThread myThread = new MyThread();
Thread t1 = new Thread(myThread,"1");
Thread t2 = new Thread(myThread,"2");
Thread t3 = new Thread(myThread,"3");
Thread t4 = new Thread(myThread,"4");
Thread t5 = new Thread(myThread,"5");
t1.start();
t2.setPriority(8);
t2.start();
t3.setPriority(10);
t3.start();
t4.setPriority(9);
t4.start();
}
}
class MyThread implements Runnable {
int ticket = 60 ;
/* */
@Override
public void run() {
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized((Object)ticket) {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "=>" + Thread.currentThread().getPriority() + " ticket" + ticket--);
if (ticket % 4 == 0)
System.out.println("------");
} else
break;
}
}
}
/*
@Override
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!func()){
break;
}
if(ticket%4==0)
System.out.println("------");
}
}
private synchronized boolean func() {
if(ticket>0){
System.out.println(Thread.currentThread().getName()+ "=>" + Thread.currentThread().getPriority()+" ticket"+ticket-- );
return true;
}else
return false;
}
*/
}