读书人

不同同步区域锁的有关问题

发布时间: 2012-10-30 16:13:36 作者: rapoo

不同同步区域锁的问题

class Ticket implements Runnable//extends Thread //{private static int tick = 100;//Object obj  = new Object();boolean b = true;public  void run(){if(b)while(true)show();else{while(true){synchronized(Ticket.class){if(tick>0){try{Thread.sleep(10);}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"...code....:"+tick--);}}}}}public static synchronized void show(){if(tick>0){try{Thread.sleep(10);}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"....show...:"+tick--);}}}class ThisLock {public static void main(String[] args) {Ticket t = new Ticket();Thread t1 = new Thread(t);Thread t2 = new Thread(t);t1.start();try{Thread.sleep(10);}catch(Exception e){}t.b = false;t2.start();}}
?

读书人网 >编程

热点推荐