首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-10-30 
不同同步区域锁的问题class Ticket implements Runnable//extends Thread //{private static int tick 1

不同同步区域锁的问题

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

热点排行