package com.test;
public class testTread {
public static void main(String[] args){
myThread1 thread=new myThread1();
Thread t1=new Thread(thread,"窗口1");
Thread t2=new Thread(thread,"窗口2");
Thread t3=new Thread(thread,"窗口3");
t1.start();
t2.start();
t3.start();
}
static class myThread1 implements Runnable{
static int tickets=100;
@Override
public void run() {
// TODO Auto-generated method stub
while(true){//注意加循环
synchronized (this){//加上锁,解决读取脏数据,数据重复,无序的问题
if(tickets>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"售出第"+tickets--+"张");
}
}
}
}
}
static class myThread2 implements Runnable{
@Override
public void run(){
System.out.println("thread2");
}
}
static class myThread3 implements Runnable{
public void run(){
System.out.println("thread3");
}
}
}