package work2;
public class SynchronizedTest implements Runnable {
int num = 50;
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
synchronized (this) {
//所执行的代码块
/
if (num > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "--票数" + num--);
}
/
if (num <= 0 || num >50 ) {
break;
}
}
}
}
public static void main(String[] args) {
SynchronizedTest t = new SynchronizedTest();
Thread tA = new Thread(t, "线程一");
Thread tD = new Thread(t, "线程四");
Thread tB = new Thread(t, "线程二");
Thread tC = new Thread(t, "线程三");
tA.start();
tB.start();
tC.start();
tD.start();
}
}