第五章java中notify和notifyAll的区别|
notify
在这篇文章中,我们将看到 Java和Java 之间的区别notifyall
。
通知():
当您在对象上调用notify]方法时,它会唤醒等待该对象的线程之一。因此,如果多个线程正在等待一个对象,它将唤醒其中一个。现在你一定想知道它会唤醒哪一个。它实际上取决于操作系统的实现。
通知所有():
notifyAll 将唤醒等待该对象的所有线程,不像 notify 只唤醒其中一个。哪个将首先唤醒取决于线程优先级和操作系统实现。
让我们借助示例来理解它:
1. 创建一个名为 File.java 的类:
线程将在其上执行操作并调用等待和通知方法是 java bean 类。
package org.arpit.java2blog.thread;
public class File {
String name;
boolean isCompleted;
public File(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isCompleted() {
return isCompleted;
}
public void setCompleted(boolean isCompleted) {
this.isCompleted = isCompleted;
}
}
2. 创建一个名为 FileReader.java 的类
这个线程会一直等到其他线程调用notify方法,然后才会完成它的处理。它将首先锁定文件对象,并从同步块中调用。因此在此示例中,它将等待 FileWriter 完成文件。
package org.arpit.java2blog.thread;
public class FileReader implements Runnable{
File file;
public FileReader(File file) {
super();
this.file = file;
}
@Override
public void run() {
synchronized (file) {
System.out.println(Thread.currentThread().getName()+" is waiting for the file to be completed: "+file.getName());
try {
file.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+": File has been completed now!! you can read it");
}
}
}
3. 创建一个名为 FileWriter.java 的类
此类将通知正在等待文件对象的线程(在通知的情况下)。它不会在调用 notify 后立即放弃锁,它首先完成其同步块。所以在本例中,FileWriter 将完成文件并将其通知给 FileReaders。
package org.arpit.java2blog.thread;
public class FileWriter implements Runnable{
File file;
public FileWriter(File file) {
super();
this.file = file;
}
@Override
public void run() {
synchronized (file) {
System.out.println("Write is going to start writing the file : " +file.getName() );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
file.setCompleted(true);
System.out.println("File has been completed now");
file.notify();
System.out.println("notify one reader");
}
}
}
4. 创建类NotifyAndNotifyAllMain,java。
这是我们的主类,它将创建上述类的对象并运行它。
package org.arpit.java2blog.thread;
public class NotifyAndNotifyAllMain {
public static void main(String args[])
{
// File object on which wait and notify method will be called
File file=new File("Excel file");
FileReader reader1=new FileReader(file);
FileReader reader2=new FileReader(file);
// FileReader threads which will wait for completion of file
Thread thread1=new Thread(reader1,"Reader 1");
Thread thread2=new Thread(reader2,"Reader 2");
thread2.start();
thread1.start();
// To ensure both readers started waiting for the file
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// FileWriter thread which will notify once file get completed
FileWriter fileWriter=new FileWriter(file);
Thread fileWriterThread=new Thread(fileWriter);
fileWriterThread.start();
}
}
在通知()的情况下:
当您运行上述程序时,您将获得以下输出:
Reader 2 is waiting for the file to be completed: Excel file
Reader 1 is waiting for the file to be completed: Excel file
Write is going to start writing the file : Excel file
File has been completed now
notify one reader
Reader 2: File has been completed now!! you can read it
所以在这里,两个 FileReader 线程(阅读器 1 和阅读器 2)正在等待文件完成,所以他们调用了 file.wait()。一旦 FileWriter 完成它的文件,它就会调用 file.notify() 并且 reader 2 线程启动并完成其处理。
在 notifyAll() 的情况下:
让我们将 FileWriter 类更改为调用 file.notifyAll()。
package org.arpit.java2blog.thread;
public class FileWriter implements Runnable{
File file;
public FileWriter(File file) {
super();
this.file = file;
}
@Override
public void run() {
synchronized (file) {
System.out.println("Write is going to start writing the file : " +file.getName() );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
file.setCompleted(true);
System.out.println("File has been completed now");
file.notifyAll();
System.out.println("notify all readers");
}
}
}
当你运行上面的程序时,你会得到以下输出:
Reader 2 is waiting for the file to be completed: Excel file
Reader 1 is waiting for the file to be completed: Excel file
Write is going to start writing the file : Excel file
File has been completed now
notify all readers
Reader 1: File has been completed now!! you can read it
Reader 2: File has been completed now!! you can read it
在 notifyAll() 的情况下,它会通知所有等待该对象的线程。
相关文章:
- [其它]第六章JavaFutureTask示例|
- [其它]第六章JavaScheduledThreadPoolExecutor示例|
- [其它]第六章使用Callable和Future的JavaExecutorService示例|
- [其它]第六章JavanewCachedThreadPool示例|
- [其它]第六章JavanewFixedThreadPool示例|
- [其它]第六章:为什么我们需要Executor框架?|
- [其它]第五章在java中使用3个线程打印序列|
- [其它]第五章我们可以在java中启动一个线程两次吗|
- [其它]第五章java中的对象级锁定与类级锁定|
- [其它]第五章Java线程连接示例|
相关推荐:
- [其它]第三章java中的抽象类|
- [其它]第四章java中的TreeMap与示例|
- [其它]第四章java中的hashcode()和equals()方法|
- [其它]第六章JavaFutureTask示例|
- [其它]计算机组成原理PDF扫描格式电子版百度云网盘下载
- [其它]程序设计语言概念(第9版)[Robert W. Sebesta]pdf格式电子版百度云网盘下载[53.8M]
- [其它]程序员必读之软件架构[Simon Brown]pdf格式电子版百度云网盘下载[41.6M]
- [其它]程序员的思维修炼 开发认知潜能的九堂课[Andy Hunt]pdf格式电子版百度云网盘下载
- [其它]程序员的职业素养[Robert C. Martin]pdf格式电子版百度云网盘下载[21.6M]
- [其它]程序员教程(第3版)[张淑平]pdf格式电子版百度云网盘下载[136.5M]
- 计算机组成原理PDF扫描格式电子版百度云网盘下载
- 硅谷之谜[吴军]pdf格式电子版百度云网盘下载[59.5M]
- 七周七语言 理解多种编程范型[Bruce A. Tate]pdf格式电子版百度云网盘下载[9M]
- 系统集成项目管理工程师考试考眼分析与样卷解析(2014版)[软考新大纲研究组]
- 自选基金助手,一款Chrome扩展程序
- 程序员面试金典(第5版)[Gayle Laakmann McDowell]pdf格式电子版百度云网盘下载[81.5M]
- 创业维艰 如何完成比难更难的事[Ben Horowitz]pdf格式电子版百度云网盘下载[65.3
- 程序员的思维修炼 开发认知潜能的九堂课[Andy Hunt]pdf格式电子版百度云网盘下载
- 图灵的秘密 他的生平、思想及论文解读[Charles Petzold]pdf格式电子版百度云网盘下
- OCP OCA认证考试指南全册:Oracle Database 11g(1Z0-051、1Z0-052、1Z0-053)[John Watson]pdf格式