Java之wait、await、sleep的区别和联系

一、wait() notify() notifyAll()

调用wait()使得线程等待某个条件满足,线程在等待时会被挂起,当其他线程运行时使得这个条件满足时,其他线程会调用notify()或者notifyAll()来唤醒该挂起的线程。

wait()挂起期间,线程会释放锁。假若线程没有释放锁,那么其他线程就无法进入对象的同步方法或同步控制块中,也就无法执行notify() 和 notifyAll()方法来唤醒挂起的线程,从而造成死锁。

使用示例:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Thread_wait_notify {
    public static void main(String[] args){
        ExecutorService executorService = Executors.newCachedThreadPool();
        WaitNotifyExample exam = new WaitNotifyExample();
        executorService.execute(() -> exam.after());
        executorService.execute(() -> exam.before());
    }
}
class WaitNotifyExample{
    private Lock lock = new ReentrantLock();

    public synchronized void before() {
        System.out.println("before func");
        notifyAll();
        // 唤醒所有线程
    }

    public synchronized void after(){
        try {
            wait();
            // 执行wait() 操作将线程挂起,该线程会释放锁。 否则其他线程无法进入对象的同步方法或者同步控制块中,
            // 就无法执行notify()或notifyAll() 就会造成 死锁
        } catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("after func");
    }
}

输出: 

二、wait() 和 sleep() 的区别

同:

1. 都是线程同步时会用到的方法,使当前线程暂停运行,把机会交给其他线程

2.如果任何线程在等待期间被中断都会抛出InterruptedException

3.都是native() 方法

异:

1. wait() 是Object超类中的方法;而sleep()是线程Thread类中的方法

2. 对锁的持有不同,wait()会释放锁,而sleep()并不释放锁

3. 唤醒方法不完全相同,wait() 依靠notify或者notifyAll 、中断、达到指定时间来唤醒;而sleep()到达指定时间被唤醒.

4. 使用位置不同,wait只能在同步代码块或同步控制块中使用,而sleep可以在任何位置使用

 

三、await() signal() 和 signalAll()

java.util.concurrent类库中提供的Condition类来实现线程之间的协调。

在Condition上调用 await() 方法使线程等待,其他线程调用signal() 或 signalAll() 方法唤醒等待的线程。

ReentrantLock里面默认有实现newCondition()方法,新建一个条件对象。

下面使用Lock来获取一个Condition对象,使用重入锁ReentrantLock

代码示例:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Thread_await_signal {
    public static void main(String[] args){
        ExecutorService executorService = Executors.newCachedThreadPool();
        AwaitSignalExample exam = new AwaitSignalExample();
        executorService.execute(() -> exam.after());
        executorService.execute(() -> exam.before());
    }
}
// java.util.concurrent类库中提供了Condition类来实现线程之间的协调,可以在Condition上调用await()方法使线程挂起。
// 其他线程可以调用signal()或signalAll()来唤醒等待的线程.
// 使用Lock来获取一个Condition对象
class AwaitSignalExample{

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void before() {
        lock.lock();
        try {
            System.out.println("before func");
            condition.signalAll();
            // 唤醒挂起的其他线程
        } finally {
            lock.unlock();
        }
    }

    public void after() {
        lock.lock();
        try {
            condition.await();
            System.out.println("after func");
        } catch (InterruptedException e){
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

结果输出:

 

四、wait() 与 await()

1. wait()是Object超类中的方法,而await()是ConditionObject类里面的方法.

2. await会导致当前线程被阻塞,会释放锁,这点和wait是一样的

3. await中的lock不再使用synchronized把代码同步包装起来

4. await的阻塞需要另外的一个对象condition

5. notify是用来唤醒使用wait的线程;而signal是用来唤醒await线程。

6. 所在的超类不同使用场景也不同,wait一般用于Synchronized中,而await只能用于ReentrantLock锁中

 

参考博客:

https://blog.csdn.net/boguesfei/article/details/81736489

https://www.cnblogs.com/java-learner/p/9652027.html

https://cyc2018.github.io/CS-Notes

  • 18
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Java并发编程是指在多个线程同时运行时,对共享资源的访问和修改进行协调和管理,以确保程序的正确性和性能。 下面是几个常见的Java并发场景和相应的实现代码: 1. 生产者-消费者模型 生产者-消费者模型是一种常见的并发场景,它涉及到一个或多个生产者线程向一个共享的缓冲区中生产数据,同时一个或多个消费者线程从缓冲区中消费数据。为了协调生产者和消费者线程之间的访问和修改,需要使用锁、条件变量等并发控制机制。 以下是一个简单的生产者-消费者模型的Java实现代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Random; public class ProducerConsumerExample { public static void main(String[] args) { Queue<Integer> buffer = new LinkedList<>(); // 缓冲区 int maxSize = 10; // 缓冲区最大容量 Thread producerThread = new Thread(new Producer(buffer, maxSize), "Producer"); Thread consumerThread = new Thread(new Consumer(buffer), "Consumer"); producerThread.start(); consumerThread.start(); } static class Producer implements Runnable { private Queue<Integer> buffer; private int maxSize; public Producer(Queue<Integer> buffer, int maxSize) { this.buffer = buffer; this.maxSize = maxSize; } @Override public void run() { while (true) { synchronized (buffer) { while (buffer.size() == maxSize) { try { buffer.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Random random = new Random(); int number = random.nextInt(100); buffer.add(number); System.out.println("Produced " + number); buffer.notifyAll(); } } } } static class Consumer implements Runnable { private Queue<Integer> buffer; public Consumer(Queue<Integer> buffer) { this.buffer = buffer; } @Override public void run() { while (true) { synchronized (buffer) { while (buffer.isEmpty()) { try { buffer.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } int number = buffer.poll(); System.out.println("Consumed " + number); buffer.notifyAll(); } } } } } ``` 2. 线程线程池是一种管理线程的机制,它通过在应用程序启动时创建一定数量的线程并将它们放入池中,然后在应用程序运行期间重复使用这些线程,以避免因频繁创建和销毁线程而导致的性能问题。Java中提供了Executor和ThreadPoolExecutor两个类来实现线程池。 以下是一个简单的线程池的Java实现代码: ```java import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadPoolExample { public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(5); for (int i = 0; i < 10; i++) { Runnable worker = new WorkerThread("Task " + i); executor.execute(worker); } executor.shutdown(); while (!executor.isTerminated()) {} System.out.println("All tasks completed"); } static class WorkerThread implements Runnable { private String taskName; public WorkerThread(String taskName) { this.taskName = taskName; } @Override public void run() { System.out.println(Thread.currentThread().getName() + " " + taskName + " is running"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " " + taskName + " is completed"); } } } ``` 3. CountDownLatch CountDownLatch是一种同步工具,它允许一个或多个线程等待一组事件的完成。当CountDownLatch的计数器变为0时,等待线程可以继续执行。Java中提供了CountDownLatch类来实现这种同步机制。 以下是一个简单的CountDownLatch的Java实现代码: ```java import java.util.concurrent.CountDownLatch; public class CountDownLatchExample { public static void main(String[] args) throws InterruptedException { int n = 5; CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(n); for (int i = 0; i < n; i++) { new Thread(new Worker(startSignal, doneSignal)).start(); } System.out.println("Workers are waiting for the start signal"); Thread.sleep(1000); startSignal.countDown(); // 发送开始信号 System.out.println("Workers are started"); doneSignal.await(); // 等待所有任务完成 System.out.println("All tasks are completed"); } static class Worker implements Runnable { private final CountDownLatch startSignal; private final CountDownLatch doneSignal; public Worker(CountDownLatch startSignal, CountDownLatch doneSignal) { this.startSignal = startSignal; this.doneSignal = doneSignal; } @Override public void run() { try { startSignal.await(); // 等待开始信号 System.out.println(Thread.currentThread().getName() + " is working"); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } finally { doneSignal.countDown(); // 发送完成信号 } } } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值