读书人

java 线程共享有关问题

发布时间: 2012-10-11 10:16:10 作者: rapoo

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(); }}

?

读书人网 >编程

热点推荐