重复测试条件直到它变为真的过程称为轮询Polling?。
轮询通常在循环的帮助下实现,以检查特定条件是否为真。如果是,则采取某些行动。这浪费了许多CPU周期并使实现效率低下。
例如,在一个经典的排队问题中,一个线程正在生成数据而另一个正在使用它。
为了避免轮询,Java使用三种方法,即wait(),notify()和notifyAll()。
所有这些方法都属于对象类,因此所有类都有它们。它们必须仅在同步块中使用。
请注意,这个程序可能只在离线IDE中运行,因为它包含在几个点上输入。
// Java program to demonstrate inter-thread communication // (wait(), join() and notify()) in Java import java.util.Scanner; public class threadexample { ????public static void main(String[] args) ???????????????????????????throws InterruptedException ????{ ????????final PC pc = new PC(); ????????// Create a thread object that calls pc.produce() ????????Thread t1 = new Thread(new Runnable() ????????{ ????????????@Override ????????????public void run() ????????????{ ????????????????try ????????????????{ ????????????????????pc.produce(); ????????????????} ????????????????catch(InterruptedException e) ????????????????{ ????????????????????e.printStackTrace(); ????????????????} ????????????} ????????}); ????????// Create another thread object that calls ????????// pc.consume() ????????Thread t2 = new Thread(new Runnable() ????????{ ????????????@Override ????????????public void run() ????????????{ ????????????????try ????????????????{ ????????????????????pc.consume(); ????????????????} ????????????????catch(InterruptedException e) ????????????????{ ????????????????????e.printStackTrace(); ????????????????} ????????????} ????????}); ????????// Start both threads ????????t1.start(); ????????t2.start(); ????????// t1 finishes before t2 ????????t1.join(); ????????t2.join(); ????} ????// PC (Produce Consumer) class with produce() and ????// consume() methods. ????public static class PC ????{ ????????// Prints a string and waits for consume() ????????public void produce()throws InterruptedException ????????{ ????????????// synchronized block ensures only one thread ????????????// running at a time. ????????????synchronized(this) ????????????{ ????????????????System.out.println("producer thread running"); ????????????????// releases the lock on shared resource ????????????????wait(); ????????????????// and waits till some other method invokes notify(). ????????????????System.out.println("Resumed"); ????????????} ????????} ????????// Sleeps for some time and waits for a key press. After key ????????// is pressed, it notifies produce(). ????????public void consume()throws InterruptedException ????????{ ????????????// this makes the produce thread to run first. ????????????Thread.sleep(1000); ????????????Scanner s = new Scanner(System.in); ????????????// synchronized block ensures only one thread ????????????// running at a time. ????????????synchronized(this) ????????????{ ????????????????System.out.println("Waiting for return key."); ????????????????s.nextLine(); ????????????????System.out.println("Return key pressed"); ????????????????// notifies the produce thread that it ????????????????// can wake up. ????????????????notify(); ????????????????// Sleep ????????????????Thread.sleep(2000); ????????????} ????????} ????} }
输出:
producer thread running Waiting for return key. Return key pressed Resumed
虽然看起来很怪异,但如果你经历两次它真的是小菜一碟。
让我们了解我们的产品和消费方法是如何工作的。
如果您仍然对我们在使用消费线程中使用notify的原因感到困惑,请尝试删除它并再次运行程序。正如您现在必须注意到的,该程序永远不会终止。
原因很简单 - 当您在生产线程上调用wait时,它继续等待并且从未终止。由于程序运行直到其所有线程都已终止,因此它会一直运行。
这个问题还有第二种方法。您可以使用wait()的第二个变体。
void wait(long timeout)
这将使调用线程仅在指定的时间内休眠。
??Java 多线程原文:https://www.cnblogs.com/breakyizhan/p/13287111.html