如下代码:Thread.sleep()方法为什么只能try异常而不能抛异常呢,synchronized()中加this不行吧,加this四个线程就不用同一把锁了吧,求大神指教
package com.innerclass.thread;
class Ticket3 implements Runnable {
private int ticket = 100;
public void run() {
while(true) {
synchronized(this) {
if(ticket > 0) {
try {
Thread.sleep(10);
}catch(Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "::" + ticket--);
}
}
}
}
}
public class TicketDemo3 {
public static void main(String[] args) {
Ticket3 ticket3 = new Ticket3();
new Thread(ticket3).start();
new Thread(ticket3).start();
new Thread(ticket3).start();
new Thread(ticket3).start();
}
}
|