java中_wait与notify及线程同步总结

wait重载方法三种
wait(lang),wait(),wait(long,int)
对于上面三种方法最终都是调用下面代码,不同的是
wait()相当于wait(0),只能等待notify或notify最终唤醒
而wait(lang)和wait(long,int)是设置wait(timeout)也就是超时时间;

public final native void wait(long timeout) throws InterruptedException;

wait与sleep最大的区别

  1. sleep不会释放lock,而wait会释放,而且会加入等待队列。
  2. sleep不需要被唤醒(休眠之后退出阻塞),但是wait不设置超时时间的话需要被中断。

在调用wait方法时,线程必须要持有被调用对象的锁,当调用wait方法后,线程就会释放掉该对象的锁(monitor)
在调用Thread类的sleep方法时,线程是不会释放掉对象的锁的

关于wait与notify和notifyAll方法的总结

  1. 当调用wait时,首先需要确保调用了wait方法的线程已经持有了对象的锁
  2. 当调用wait后,该线程就会释放掉这个对象的锁,然后进入等待状态(wait set)
  3. 当线程调用了wait后进入到等待状态时,它就可以等待其他线程调用相同对象的notify或notityAll方法来使自己被唤醒
  4. 一旦这个线程被其他线程唤醒后,该线程就会与其他线程一同开始竞争这个对象的锁(公平竞争),只用当该线程获取到了这个对象的锁后,线程才会继续往下执行
  5. 调用wait方法的代码片段需要放在一个synchronized块或是synchronized方法中,这样菜可以确保线程在调用wait方法前已经获取到了对象的锁
  6. 当调用对象的notify方法时,它会随机唤醒该对象等待集合(wait set)中的任意一个线程。

当对象被synchronized修饰,Test对象调用methd1或method2这两个方法是互不干扰的,因为锁的是同一个对象,所以method1方法未执行完,method2方法就不能执行

public class Test {
    public synchronized void method1(){
    
    }
    
    public synchronized void method2(){
    }
    public static void main(String[] args) {
        Test test = new Test();
        
    }
}

但是当创建两个对象,当第一个对象执行method1时,第二个线程可以执行第二个对象的method2,

public class Test {
    public synchronized void method1(){
    
    }
    
    public synchronized void method2(){
    }
    public static void main(String[] args) {
        Test test = new Test();
         Test test2 = new Test();
    }
}

以下代码中当method1未执行,是可以执行method2的。因为method1方法锁的是当前Test对象,method2方法锁的是当前对象的class对象

public class Test {
    public synchronized void method1(){
    }
    
    public static synchronized void method2(){
        
    }
    public static void main(String[] args) {
        Test test = new Test();
    }
}

增加一个练习

  1. 存在一个对象,该对象有一个int类型的成员变量counter,该成员变量的初始值为0.
  2. 创建两个线程,其中一个线程对该对象的成员变量counter增1,另一个线程对该对象的成员变量减1.
  3. 输出该对象成员变量counter每次变化后的值。
  4. 最终输出结果应为:1010101010101…

核心代码

public class Test {

    private static int counter = 0;

    public synchronized void method1() throws InterruptedException {
        if(counter !=0){
            wait();
        }
         counter++;

        System.out.println(counter);

        notify();

    }

    public synchronized void method2(){

        if(counter == 0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        counter--;
        System.out.println(counter);
        notify();
    }
}

线程一

public class method1 extends Thread{

    private Test test;

    public method1(Test test){
        this.test = test;
    }

    @SneakyThrows
    @Override
    public void run() {
        Thread.sleep((long) (Math.random()*1000));
        for(int i=0; i<30;++i){
            test.method1();
        }
    }
}

线程二

public class method2 extends Thread{
    private Test test;

    public method2(Test test){
        this.test = test;
    }

    @SneakyThrows
    @Override
    public void run() {
       for (int i=0;i<30;++i){
           Thread.sleep((long) (Math.random()*1000));
           Thread.sleep(100);
           test.method2();
        }
    }
}

测试类

public class experiment {
    public static void main(String[] args) {
        Test test = new Test();
        method1 method1 = new method1(test);
        method2 method2 = new method2(test);
        method1.start();
        method2.start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值