本帖最后由 王海龙 于 2012-12-24 12:38 编辑
- public class Demo implements Runnable {
- private int ticket = 100;
- Object obj = new Object();
- @Override
- public void run() {
- synchronized (obj) {
-
-
- do {
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName() + "***"
- + ticket--);
- } while (ticket > 0);}
- }
- public static void main(String[] args) {
- Demo d = new Demo();
- Thread t1 = new Thread(d);
- Thread t2 = new Thread(d);
- Thread t3 = new Thread(d);
- Thread t4 = new Thread(d);
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
复制代码 上边这段代码和毕老师的差不多,但是为什么还是会出现ticket<0的情况?
|