java 线程共享问题

package thread;
/**
 * 实现Runnable 可资源共享
 * @author lixunhui

 */
public class MyThread5 implements Runnable {
 private int ticket=5;
 @Override
 public synchronized void run() {
  for(int i=0;i<100;i++){
   if(ticket>0){
    try {
     Thread.sleep(1000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    System.out.println("Bye ticket.."+ticket--);
   }
  }
 }
 public static void main(String[] args) {
  MyThread5 thread=new MyThread5();
  Thread t1=new Thread(thread);
  Thread t2=new Thread(thread);
  t1.start();
  t2.start();
 }
}

 

你可能感兴趣的:(java)