public class ThreadTest extends Thread{
public static volatile Integer ticketCount=50;
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("线程号:"+Thread.currentThread().getName()+",票剩余"+getTicketCount());
}
public synchronized Integer getTicketCount() {
ticketCount=ticketCount-1;
return ticketCount;
}
}
//测试类方法
@org.junit.Test
public void testThread() {
ThreadTest threadTest1=new ThreadTest();
ThreadTest threadTest2=new ThreadTest();
ThreadTest threadTest3=new ThreadTest();
ThreadTest threadTest4=new ThreadTest();
//1可以多线程共享数据
// new Thread(threadTest1,“1号”).start();
// new Thread(threadTest1,“2号”).start();
// new Thread(threadTest1,“3号”).start();
// new Thread(threadTest1,“4号”).start();
//==2不能多线程共享数据 ==
new Thread(threadTest1,“1号”).start();
new Thread(threadTest2,“2号”).start();
new Thread(threadTest3,“3号”).start();
new Thread(threadTest4,“4号”).start();
}