Java beginners to proficient-the multi-thread of basic articles to achieve simple PV operation process synchronization, the entry to proficient pv
I. Overview
PV operations are operations on semaphores.
Process Synchronization is a type of constraint between concurrent processes. Execution of a process depends on messages of another process. When a process does not receive messages from another process, it must wait, the message is awakened only when the message arrives. For example: like the relationship between consumers and producers, it is said that eating steamed bread has a bowl that can hold six steamed bread. The cook keeps producing steamed bread, and the customer keeps eating steamed bread, in this case, the relationship between the cook and the customer is synchronized. (For example)
Next I will use java multi-threaded code to implement this operation.
Ii. Code display.
1. ManTou. java
package com.gaowei.PV;public class ManTou {int id; ManTou(int id) {this.id = id;}public String toString() {return "ManTou : " + id;}}
2. Chef class Pclient. java
Package com. gaowei. PV; public class PClient implements Runnable {private SyncStack ss = null; public PClient (SyncStack ss) {this. ss = ss ;}@ Overridepublic void run () {for (int I = 0; I <20; I ++) {ManTou mt = new ManTou (I); ss. push (mt); System. out. println ("produced:" + mt); try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace ();}}}}
3. Customer Vclient. java
Package com. gaowei. PV; public class VClient implements Runnable {private SyncStack ss = null; public VClient (SyncStack ss) {this. ss = ss ;}@ Overridepublic void run () {for (int I = 0; I <20; I ++) {ManTou mt = ss. pop (); System. out. println ("consumed:" + mt); try {Thread. sleep (6000);} catch (InterruptedException e) {e. printStackTrace ();}}}}
4. Bowl-class SyncStack. java
Package com. gaowei. PV; public class SyncStack {private int index = 0; ManTou [] arrMT = new ManTou [6]; // Add Shantou public synchronized void push (ManTou mt) {while (index = arrMT. length) {try {this. wait ();} catch (InterruptedException e) {e. printStackTrace () ;}} this. policyall (); arrMT [index] = mt; index ++;} // eat steamed bread public synchronized ManTou pop () {while (index = 0) {try {this. wait ();} catch (InterruptedException e) {e. printStackTrace () ;}} this. policyall (); index --; return arrMT [index] ;}}
5. Client display result code PVClient. java
package com.gaowei.PV;public class PVClient {public static void main(String[] args) {SyncStack ss = new SyncStack();PClient p = new PClient(ss);VClient v= new VClient(ss);new Thread(p).start();new Thread(v).start();}}
6,
Because the customer needs to eat steamed buns for 6 seconds, the chefs can make a steamed bread for 1 s, so we can find that the chefs have made 6th steamed bread, and the consumers have eaten 1 steamed bread. Next, they can only hold 6 steamed bread in the bowl. therefore, the cook only needs to wait for the customer to eat and then put a steamed bread in the bowl, until the cook's 20 steamed buns are finished, the customer must continue to finish the remaining 6 steamed buns.
Iii. Summary.
After using multithreading to implement PV operations, we need to think about the benefits of PV operations? In the process of implementing the Code, a detailed problem is mentioned. When a customer eats a steamed bread, the thread will be notified to the cook to produce the steamed bread. When the cook produces a steamed bread, it will also notify the customer to eat the steamed bread. This approach is similar to the observer approach to solve the coupling between producers and consumers. It is said that the producer is doing his own thing, the consumer is doing his own thing, the message in the bowl is used to notify the producer what to do, and the consumer should do it. In this way, the relationship between producers and consumers is decoupled.