Iv. collaboration, mutual exclusion, collaboration--java multithreaded collaboration (wait, notify, Notifyall)
The Java monitor supports two threads: mutual exclusion and collaboration.
We have described the mutual exclusion implemented by object locking and reentrant locking. In this article, let's look at the collaboration of threads.
For example: There is a burger-eating competition in Hamburg, with 3 customers in the final, 3 cooks, and a waiter coordinating the number of hamburgers. To avoid waste, the hamburger is put into a long, 10-burger container, followed by the first-come-and-go principle. If the container is filled, the chef stops making burgers, and if the customer finds that the hamburger in the container is finished, it can shoot the alarm on the container and remind the chef to make a few more burgers. At this time the waiter came to reassure the customer, let him wait. Once the chef's burger is made, the waiter informs the customer that the burger is ready and the customer continues to come and get the burger.
Here, the customer is actually what we call the consumer, and the chef is the producer. The container is the monitor that determines the chef's behavior, and the waiter is responsible for monitoring the customer's behavior.
In the JVM, this type of monitor is called a wait-and-wake Monitor.
In this monitor, a thread that already holds the monitor can suspend its execution by invoking the wait method of the monitor object and releasing the monitor itself into a waiting area until another thread in the monitor invokes the Notify method of the monitor object. When a thread invokes the wake command, it continues to hold the monitor until it actively releases the monitor. After that, the waiting line Cheng wakes up, and one of them regains the monitor to determine the condition state, to decide whether to proceed to the wait state or to perform the monitoring area, or exit.
Take a look at the following code:
1.public class Notifytest {
2.private String flag = "true";
3.
4.class Notifythread extends thread{
5.public Notifythread (String name) {
6.super (name);
7.}
8.public void Run () {
9.try {
10.sleep (3000);//Postpone 3 second notice
One.} catch (Interruptedexception e) {
12.e.printstacktrace ();
13.}
14.
15.flag = "false";
16.flag.notify ();
17.}
18.};
19.
20.class Waitthread extends Thread {
21.public Waitthread (String name) {
22.super (name);
23.}
24.
25.public void Run () {
26.
27.while (flag!= "false") {
28.system.out.println (GetName () + "begin waiting!");
29.long waittime = System.currenttimemillis ();
30.try {
31.flag.wait ();
catch (Interruptedexception e) {
33.e.printstacktrace ();
34.}
35.waitTime = System.currenttimemillis ()-waittime;
36.system.out.println ("Wait Time:" +waittime);
37.}
38.system.out.println (GetName () + "End waiting!");
39.
40.}
41.}
42.
43.public static void Main (string[] args) throws Interruptedexception {
44.system.out.println ("Main Thread run!");
45.NotifyTest test = new Notifytest ();
46.NotifyThread notifythread =test.new notifythread ("Notify01");
47.WaitThread waitThread01 = test.new waitthread ("Waiter01");
48.WaitThread waitThread02 = test.new waitthread ("Waiter02");
49.WaitThread waitThread03 = test.new waitthread ("waiter03");
50.notifythread.start ();
51.waitthread01.start ();
52.waitthread02.start ();
53.waitthread03.start ();
54.}
55.
56.}