本帖最后由 王浩龙 于 2014-3-15 17:28 编辑
- class Ticket implements Runnable{
- private int tick = 1000;
- Object obj = new Object();
- boolean flag = true;
- public void run(){
- if(flag){
- while(true){
- //这里是不是有三个锁呢?自己的锁加上show()的锁,show()又有synchronized(obj)
- synchronized(obj){
- show();
- }
- }
- }
- else
- while(true)
- show();
- }
- public synchronized void show(){
- synchronized(obj){
- if(tick>0){
- try{Thread.sleep(10);}catch(Exception e){}
- System.out.println(Thread.currentThread().getName()+"....code : "+ tick--);
- }
- }
- }
- }
- class DeadLockDemo{
- 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.flag = false;
- t2.start();
- }
- }
复制代码 我能理解毕老师讲的死锁的问题,就是互相拿着对方的锁不放开。里边我有一点疑惑就是到底有几个锁
请看下面的代码
|