java多线程 中wait notify验证代码

package ThreadTest;

/**
 * Created by Administrator on 2016/11/2.
 */
public class Service {
    public void testMethod(Object lock){
        try{
            synchronized(lock){
               System.out.println("wait begin");
                Thread.sleep(400);
                System.out.println("end wait");
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

package ThreadTest;

import javaThread.Threadd;

/**
 * Created by Administrator on 2016/11/2.
 */
public class ThreadA extends Thread{
    private Object lock;
    public ThreadA(Object lock){
        this.lock=lock;
    }
    public void run(){
        Service service=new Service();
        service.testMethod(lock);
    }
}

package ThreadTest;

/**
 * Created by Administrator on 2016/11/2.
 */
public class ThreadB extends Thread{
    private Object lock;
    public ThreadB(Object lock){
        this.lock=lock;
    }
    public void run(){
        Service service=new Service();
        service.testMethod(lock);
    }

 }
sleep 不释放锁
 

结果

如果把service的wait答案是

验证wait 释放锁和notify不释放锁

package ThreadTest;

/**
 * Created by Administrator on 2016/11/2.
 */
public class Service1 {
    public void testMethod(Object lock){
        try{
          synchronized(lock){
              System.out.println("begin wait threadname="+Thread.currentThread().getName());
              lock.wait();
              System.out.println("end wait threadname="+Thread.currentThread().getName());
          }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    public void synNotifyMethod(Object lock){
        try{
            synchronized(lock){
                System.out.println("notify start threadname="+Thread.currentThread().getName());
                lock.notify();
                Thread.sleep(5000);
                System.out.println("notify end threadname="+Thread.currentThread().getName());
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

package ThreadTest;

/**
 * Created by Administrator on 2016/11/2.
 */
public class ThreadC extends Thread {
    private Object lock;
    public ThreadC(Object lock){
        super();
        this.lock=lock;
    }
    public void run(){
        Service1 service1=new Service1();
        service1.testMethod(lock);
    }
}

package ThreadTest;

/**
 * Created by Administrator on 2016/11/2.
 */
public class NotifyThread extends Thread {
    private Object lock;
    public NotifyThread(Object lock){
        super();
        this.lock=lock;
    }
    public void run(){
        Service1 service=new Service1();
        service.synNotifyMethod(lock);
    }
}

package ThreadTest;

/**
 * Created by Administrator on 2016/11/2.
 * 代码结果说明必须执行notify完方法才释放锁
 * 执行完同步代码块就会释放锁
 * 在执行同步代码块的过程中,遇到异常锁也会被释放
 * 在执行代码块的过程中,执行了锁所属对象的wait方法,这个想成会直接释放锁,而此线程会进入线程等待池中等待被唤醒
 * notify之唤醒一个线程,自己下去测试 很简单  方法随机唤醒一个线程 唤醒所有线程用notifyall
 * wait(long)等待某一时间,如果超过这个时间自动唤醒
 */
public class TestNotifyAndWait {
    public static void main(String [ ] args){
        Object lock=new Object();
        ThreadC th1 = new ThreadC(lock);
        NotifyThread th2 = new NotifyThread(lock);
        th1.start();
        th2.start();
    }
}

结果

还有就是notify出现在wait前,解决方案

package ThreadTest;

/**
 * Created by Administrator on 2016/11/2.
 * 不可以通知太早  改正方法
 */
public class MyRun {
    private String lock="";
    private Runnable runnablea=new Runnable() {
        @Override
        public void run() {
            try{
                synchronized(lock){
                    System.out.println("wait start");
                    lock.wait();
                    System.out.println("end wait");
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    };
    private Runnable runnableb=new Runnable() {
        @Override
        public void run() {
            synchronized(lock){
                System.out.println("start notify");
                lock.notify();
                System.out.println("end notify");
            }
        }
    };
    public static void main(String [] args){
        MyRun run=new MyRun();
        Thread a = new Thread(run.runnablea);
        a.start();
        Thread b = new Thread(run.runnableb);
        b.start();
        MyRun runS=new MyRun();
        Thread c= new Thread(runS.runnablea);
        Thread d = new Thread(runS.runnableb);
        c.start();
        d.start();
    }
}

修改

package ThreadTest;

/**
 * Created by Administrator on 2016/11/2.
 */
public class MyRun1 {
    private String lock=new String("");
    private boolean isFirstRnuB=false;
    private Runnable runnablea=new Runnable() {
        @Override
        public void run() {
            try{
                synchronized(lock){
                    while (isFirstRnuB==false){
                    System.out.println("wait start");
                    lock.wait();
                    System.out.println("end wait");
                    }
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    };
    private Runnable runnableb=new Runnable() {
        @Override
        public void run() {
            synchronized(lock){
                System.out.println("start notify");
                lock.notify();
                System.out.println("end notify");
                isFirstRnuB=true;
            }
        }
    };
    public static void main(String [] args){
        MyRun1 runS=new MyRun1();
        Thread c= new Thread(runS.runnablea);
        Thread d = new Thread(runS.runnableb);
        d.start();
        c.start();
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值