Thread single producer single consumer and two ways

 

 

 

1 Summary:

 

 

	 * For thread deadlock, use the production-consumer model | solution to solve, in life, the shared lane resources of people and vehicles is also a producer-consumer phenomenon
	 * There are two solutions to the producer-consumer model:
	 * 1 Use a container, stop when the container is full
	      2 Signal light method, identification bit (in this way, this.wait() + this.notify() + synchronized must be used to take effect)

 

 

1.1 Compare   the code Web12306 in the description of thread multi-thread synchronization locking

In this class, locks are added to the methods in the real thread class. At the same time, the thread class holds public resource tickets, and then multiple thread proxy classes keep referencing the real thread class and call the methods of the real thread class. Consume publicly held tickets

 

The producer-consumer in 2.1 holds a common business bean in the real thread class, and then adds a lock to the method of the business bean, and then

The classes Producer and Consumer share the common object class moive, but call the production and consumption methods of the moive class respectively.

 

Note the difference

 



 

 

 

2.1 Signal light mode code:

 

 

public class ProduceConsumeThread {

	/**
	 * For thread deadlock, use the production-consumer model | solution to solve, in life, the shared lane resources of people and vehicles is also a producer-consumer phenomenon
	 * There are two solutions to the producer-consumer model:
	 * 1 Use a container, stop when the container is full
	      2 Signal light method, identification bit (in this way, this.wait() + this.notify() + synchronized must be used to take effect)
	 */
	public static void main(String[] args) {
		Moive moive = new Moive(); // Multithreading must be a reference to the same object moive for this.wait() in the moive method to take effect this.notify()
		new Thread(new Producer(moive),"cinema 1").start();// The classes Producer and Consumer share the common object class moive, but call the production and consumption methods of the moive class respectively.
		new Thread(new Consumer(moive),"Zhang San").start();// In the two methods of production and consumption of the public holding class moive, the semaphore is used to control the waiting and execution of the other party
	}
}


class Producer implements Runnable{
	
	private Moive moive;
	
	public Producer() {
		
	}
	
	public Producer(Moive moive) {
		this.moive = moive;
	}

	@Override
	public void run() {
		for(int i=0; i<20; i++) {
			if(i%2 == 0) {
				moive.play("Sky Movie");
			}else{
				moive.play("Grass Movie");
			}
		}
	}
	
}


class Consumer implements Runnable{
	
	private Moive moive; // public holding class, this class holds business methods
	
	public Consumer() {
		
	}
	
	public Consumer(Moive moive) {
		this.moive = moive;
	}

	@Override
	public void run() {
		for(int i=0; i<20; i++) {
			moive.watch();
		}
	}
	
}



class Moive{
	private String msg ;
	// When flag=true, the producer produces, the consumer waits, and the consumer is notified after production is complete
	// When flag=false, the producer waits, the consumer consumes, and the producer is notified after consumption is complete
	private boolean flag = true;
	
	// Production
	public synchronized void play(String msg) {
		if(!flag) {// wait for consumption
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace ();
			}
		}
		// start production
		try {
			Thread.sleep(1000);// Time consuming to simulate production data
		} catch (InterruptedException e) {
			e.printStackTrace ();
		}
		System.out.println(Thread.currentThread().getName() +" 生产了: " + msg);
		this.msg = msg;
		// notify consumption
		this.notify();
		this.flag = false;
	}
	
	
	// Consumption
		public synchronized void watch() {
			if(flag) {// wait for production
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace ();
				}
			}
			// start consumption
			try {
				Thread.sleep(1000);// Simulate the time spent consuming data
			} catch (InterruptedException e) {
				e.printStackTrace ();
			}
			System.out.println(Thread.currentThread().getName() +"消费了: " + msg);
			// notify consumption
			this.notify();
			this.flag = true;
		}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326524492&siteId=291194637