标题: 多线程加了同步代码块为什么还是有重复的票~求大神助攻~ [打印本页] 作者: 野生凯蒂一只 时间: 2015-5-15 00:02 标题: 多线程加了同步代码块为什么还是有重复的票~求大神助攻~ package com.homework.demo;
class Test extends Thread{
int num = 100;
Object lock = new Object();
public void run(){
while(true){
synchronized(lock){
if(num > 0){
System.out.println(Thread.currentThread().getName()+"出售编号为"+num+"的票");
num--;
}
}
}
}
}
public class HomeWork2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t1 = new Test();
t1.setName("窗口1");
t1.start();
Test t2 = new Test();
t2.setName("窗口2");
t2.start();
Test t3 = new Test();
t3.setName("窗口3");
t3.start();
}