Every object in java has the methods wait()and notify():
wait() - make the thread that call thismethod to have a sleep until other thread call this object's notify()
notify() - notify the thread that iswaiting (it is the first one that begins to wait for this object) on thisobject’s monitor that it can continue now.
When we want to coordinate some threads' actions, we can use wait() and notify() method.The following code show the easiestusage of wait() and notify, and in this
program, ThreadA needs ThreadB to let it go, or ThreadA will keep waiting for the oject's monitor.
package org.vhow.java.thread;
/**
* The resource that may be accessed by the threads that need to coordinate
* their actions.
*/
public class Data
{
boolean go = false;
public synchronized void makeWait() throws InterruptedException
{
while (!go)
{
System.out.println(Thread.currentThread() + " is waiting...");
// Let the current thread to wait until another thread invokes this.notify()
wait();
System.out.println("done.");
}
}
public synchronized void letGo()
{
go = true;
// Wakes up a single thread is waiting on this object's monitor.
notify();
}
}
package org.vhow.java.thread;
public class ThreadA extends Thread
{
Data data;
public ThreadA(Data data)
{
this.data = data;
}
@Override
public void run()
{
try
{
data.makeWait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
package org.vhow.java.thread;
public class ThreadB extends Thread
{
Data data;
public ThreadB(Data data)
{
this.data = data;
}
@Override
public void run()
{
data.letGo();
}
}
package org.vhow.java.thread;
public class AppMain
{
public static void main(String[] args) throws InterruptedException
{
// The resource that may accessed by ThreadA and ThreadB
Data data = new Data();
ThreadA threadA = new ThreadA(data);
ThreadB threadB = new ThreadB(data);
threadA.setName("thread-a");
threadB.setName("thread-b");
threadA.start();
Thread.sleep(2000);
threadB.start();
}
}
分享到:
相关推荐
Coordination Models and Languages 6th International Conference,COORDINATION 2004 Pisa Italy, February 24-27, 2004 Lecture Notes in Computer Science 2949
WS-Coordination Specification. E文版的WS-Coordination规范.
ZooKeeper Distributed process coordination
Coordination 论文资料 最新的有关coordination的资料
determined to reflect the contradictions between vehicle longitudinal and lateral dynamics. Meanwhile, some I/O constraints are formulated with a driver permissible longitudinal car-following range ...
ZooKeeper was designed to be a robust service that enables application developers to focus mainly on their application logic rather than coordination.
Wei Ren和Yongcan Cao关于多智能体系统分布式协调控制方向的经典教材。
Currency war or international policy coordination by Barry Eichengreen
WS-Coordination200411
Even with ZooKeeper, implementing coordination tasks is not trivial, but this book provides good practices to give you a head start, and points out caveats that developers and administrators alike ...
Information sharing and coordination mechanisms for managing uncertainty insupply chains a simulation study
This document defines a standard interface for power management that can ...is to ease the integration between supervisory software from different vendors working at different privilege levels.
ARM Power State and Coordination Interface (PSCI) header.
insulation coordination
LS产电Metasol MS Type 2 coordinationpdf,
DEN0022C_Power_State_Coordination_Interface.pdf psci 手册
SMT ECM Entity Coordination Management Hardware independent state machine.
RELAY COORDINATION_RelayCoordination_overcurrentrelay_overcurrent_distance_源码.rar.rar
COORDINATION 99. Coordinatio Languages and Models Third International Conference COORDINATION’99