notify在这篇文章中,我们将看到 Java和Java 之间的区别notifyall。
notify
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() 的情况下,它会通知所有等待该对象的线程。
原文链接:https://codingdict.com/