A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© moonlight 黑马帝   /  2011-11-16 17:16  /  1591 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 moonlight 于 2011-11-20 17:20 编辑

public class Deadlock {
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Dead d1=new Dead();
                Thread t1=new Thread(d1);
                Thread t2=new Thread(d1);
                t1.setName("d1");
                t2.setName("d2");
                t1.start();
                t2.start();
        }
}

class Dead implements Runnable{
        int flag;
        public void run(){
                if(Thread.currentThread().getName().equals("d1")){
                        m();       
                }
                if(Thread.currentThread().getName().equals("d2")){
                        n();
                }
        }
       
        public  synchronized void m(){
                System.out.println(Thread.currentThread().getName()+"正在用呢m()");
                try {
                        Thread.sleep(1000);
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                n();
        }
       
        public  synchronized void n(){
                System.out.println(Thread.currentThread().getName()+"正在用呢n()");
                try {
                        Thread.sleep(2000);
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }               
                m();
        }
}

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

5 个回复

倒序浏览
因为线程之间没有资源竞争。
回复 使用道具 举报
因为这个程序t1和t2不是同一个同对象,虽然你的线程类用的是同一个对象,但是你的线程还是两个线程。一个类里面所有的synchronized都是同一个锁,只要一个方法加锁了,所有方法都加锁,
这样导致你只看到一个线程在执行,其实是两个线程在执行,你可以  if(Thread.currentThread().getName().equals("d1")){
                         m();        
                }
                 if(Thread.currentThread().getName().equals("d2")){

                         n();
                 }

评分

参与人数 1技术分 +1 收起 理由
宁超 + 1

查看全部评分

回复 使用道具 举报
  你可以在方法区间打印两句话看看
回复 使用道具 举报
看我给你写的死锁,好累,希望能给加技术分

public class DeadLock implements Runnable {

        A a=new A();
        B b=new B();
        public void init(){
                Thread.currentThread().setName("main thread");
                //invoke foo method of a's instance
                a.foo(b);
                System.out.println("entered the main thread later ");
        }
        public void run() {
                // TODO Auto-generated method stub

                Thread.currentThread().setName("assistant thread");
                b.Bar(a);
                System.out.println("entered the assistant thread later");
        }
        public static void main(String[] args) {
                DeadLock dl=new DeadLock();
                new Thread(dl).start();
                //execute init method as new thread
                dl.init();
        }

}

评分

参与人数 1技术分 +2 收起 理由
宁超 + 2

查看全部评分

回复 使用道具 举报
哈哈 懂了!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马