Java 线程同步中的notify()方法示例
简介
Object 类包含notify()方法的定义。该方法只会唤醒一个正在等待项目的线程,然后该线程开始运行。可以使用线程类的notify()方法唤醒单个线程。当多个线程正在等待通知时使用notify()方法,只有一个线程会真正收到通知,并迫使其他线程继续等待。
让我们讨论线程同步中的Javanotify()方法,以及它的用法和编程示例。我们将研究它如何改进线程同步和线程间通信。
Java中notify()方法的解释
"notify()"方法属于Java中的Object类,用于促进线程间通信和协调。当线程对某个对象调用notify()时,它会唤醒对同一对象调用wait()的等待线程之一。被唤醒的线程从等待状态变为可运行状态,它将尝试重新获取对象的监视器锁以继续执行。
我们必须知道,notify()方法不会挑选要唤醒的线程。被唤醒线程的选择取决于JVM的内部实现,并且可能因每个Java运行时环境而异。如果没有线程在对象上等待,notify()方法调用将不起作用。
使用notify()方法解决卖家和客户问题
这个特定的程序属于如何使用notify()方法解决卖家和客户问题。
示例
import java.util.LinkedList;
import java.util.Queue;
public class CustomerItems
{
private final Object lock = new Object();
private Queue<Integer> buffer = new LinkedList<>();
private final int capacity = 10;
public void produce() throws InterruptedException {
synchronized (lock) {
while (buffer.size() == capacity)
{
lock.wait();
}
int items = 1;
buffer.add(items);
System.out.println("Number of Sold items: " + items);
lock.notify();
}
}
public void consume() throws InterruptedException {
synchronized (lock) {
while (buffer.isEmpty()) {
lock.wait();
}
int ValueofConsumeditem = buffer.poll();
System.out.println("Number of Consumed items: " + ValueofConsumeditem);
lock.notify();
}
}
public static void main(String args [])
{
CustomerItems example = new CustomerItems();
Thread producerThread = new Thread(() -> {
try {
while (true) {
example.produce();
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread consumerThread = new Thread(() -> {
try {
while (true) {
example.consume();
Thread.sleep(1500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
producerThread.start();
consumerThread.start();
}
}
输出
Number of Sold items: 1 Number of Consumed items: 1 Number of Sold items: 1 Number of Consumed items: 1 Number of Sold items: 1 Number of Consumed items: 1 Number of Sold items: 1 Number of Sold items: 1 Number of Consumed items: 1 Number of Sold items: 1 Number of Consumed items: 1 Number of Sold items: 1 Number of Sold items: 1 Number of Consumed items: 1 Number of Sold items: 1 Number of Consumed items: 1 Number of Sold items: 1
为此,我们首先在程序中导入了一些重要的包。
java.util.LinkedList; java.util.Queue;
之后,我们定义了名为 CustomerItems 的类。在该类中,我们创建了该特定类的对象并创建了一个链接列表。
private final Object lock = new Object(); private Queue<Integer> buffer = new LinkedList<>();
然后,我们声明了一个名为 capacity 的变量,它是 final 和整数类型,并为该特定整数分配了一些值。然后,我们调用了一个名为 produce () 的函数,该函数有一个中断执行并执行一些同步工作。
public void consumer() throws InterruptedException
如果缓冲区已满,用户必须等到缓冲区为空。如果缓冲区为空,则用户有权向此缓冲区输入一些值。现在我们只需打印用户保存在缓冲区中的已消耗值。
int ValueofConsumeditem = buffer.poll();
System.out.println("已消费商品数量:" + ValueofConsumeditem)
然后我们调用了 main() 函数,并在此函数中创建了名为 example 的对象。
CustomerItems example = new CustomerItems ();
现在我们创建了一个线程来做这个特定的工作。
ThreadproducerThread = new Thread(() ->
并调用函数example()。
example.produce(); Thread.sleep(1500);
现在我们分别启动了生产者线程和消费者线程。
producerThread.start(); consumerThread.start();
展示Java的notify()方法的使用
在这个特定的编程示例中,我们将看到notify()方法的另一种应用。
示例
public class BossThread
{
public static void main (String args [])
{
WorkerThread workerThread = new WorkerThread();
workerThread.start();
synchronized (workerThread) {
try {
// 仅显示消息
System.out.println("我们现在等待 WorkerThread 完成!!!");
// 我们为主线程调用 wait() 方法
workerThread.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
// 由WorkerThread打印结果
System.out.println("Result is: " + workerThread.getResult());
}
}
}
class WorkerThread extends Thread
{
private int output;
@Override
public void run()
{
synchronized (this)
{
for (int mp = 0; mp < 20; mp++)
{
output += mp;
}
// 使用notify()唤醒等待的线程
notify();
}
}
public int getResult() {
return output;
}
}
输出
we now wait for the WorkerThread to finish!!! Result is: 190
在这个程序中,我们首先定义了一个名为BossThread的类,并在该类中调用了main()函数。在main()函数中,我们创建了一个线程并启动了该线程。
WorkerThread workerThread = new WorkerThread(); workerThread.start();
现在我们已经完成了一些同步工作,以系统的方式完成任务是非常重要的任务。
synchronized (workerThread) {
try {
// 仅显示消息
System.out.println("我们现在等待WorkerThread完成!!!");
最后,我们只是打印了 workerthread 的结果。
System.out.println("Result is: " + workerThread.getResult());
之后,我们定义了另一个名为 WorkerThread 的类,它扩展了类线程。
class WorkerThread extends Thread
在这个类中,我们声明了一个名为"output"的变量,它是 final 和整数类型。现在我们已经执行了 run () 方法,并在 run () 函数中完成了一些同步工作。现在我们使用了notify()来唤醒等待的线程,并调用另一个名为getResult()的函数来从该函数中获取所需的输出。
public int getResult() {
return output;
}
}
结论
在本文中,我们学习了很多有关线程同步的notify()方法的配置和应用。在线程部分,同步是最重要的主题。要正确学习同步策略,我们必须详细了解notify()以涵盖整个线程主题。
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

