Java multithreaded simulation producer consumer issues, corporate interview often asked questions ...

Source: Internet
Author: User

Package com.cn.test3; Java multithreaded simulation producer consumer issues//producerconsumer is the main class, producer producers, consumer consumers, product products//storage Warehouse
Comments: I wrote the output below the program, you can look at it, in fact very easy, you imagine the product from production, to take out a production line, we define two threads, producer threads, and consumer threads, one is the producer constantly produce products and put in a limited number of designated slots, and the consumer from the designated slot in turn to remove the product, the reality of the flow shop is similar to this.
public class Test3 {public static void main (string[] args) {Storage s = new Storage (); Producer p = new Producer (s); Consumer C = new Consumer (s); Thread TP = new Thread (p); Thread TC = new thread (c); Tp.start (); Tc.start ();}} Class Consumer implements Runnable {//consumer Storage s = Null;public Consumer (Storage s) {THIS.S = s;} public void Run () {for (int i=0; i<10; i++) {//Here 10 refers to 10 products, according to the number of products produced by producers, producers produce 10 products, consumers will only be able to remove 10 products. Product P = s.pop ();//Check out products try {thread.sleep ((int) (Math.random () *1000)),} catch (Interruptedexception e) { E.printstacktrace ();}}} Class Producer implements Runnable {//producer Storage s = Null;public Producer (Storage s) {THIS.S = s;} public void Run () {//Total production of 10 products for (int i=0; i<10; i++) {//The 10 here can be arbitrarily changed to see how many products you want the producer to produce, we now assume that the production of 10 products will stop and be able to participate in the program output. Product P = new product (i); S.push (P);//put in the products//system.out.println ("producer put:" + P); try {thread.sleep ((int) (Math.random () * 1000));} catch (Interruptedexception e) {e.printstacktrace ();}}}} class Product {int id;public product (int id) {this.id = ID;}public String ToString () {//Override toString Method return "product:" +this.id;}} Class Storage {int index = 0;//The warehouse has the largest storage capacity and only has three locations. This means that the producer produces the product and puts it into the designated "slot", and we now assume that the slot has only 3 slots, of course you can set multiple, when the producer
When the product is full of 3 slots, it can only wait for the consumer to take out a slot product talent continue to produce products to put into the groove. can understand it. Product[] Products = new product[3]; Synchronized public void push (Product p) {//Put in, put and remove the code is to be locked, this can understand it. This code either does not run, or runs out ... while (Index==this.products.length) {try {this.wait ();} catch (Exception e) {e.printstacktrace ()}} This.products[index] = p; System.out.println ("producer put" +index+ "position:" + p); index++; This.notifyall ();} Public synchronized Product Pop () {//Remove while (this.index==0) {try {this.wait ();} catch (Exception e) {e.printstacktrace () ;}} index--; This.notifyall (); System.out.println ("The consumer is removed from the" + index+ "position:" + this.products[index]); return this.products[index];}}
Output Result:
Producers put in 0 position: Products: 0 consumers removed from 0 position: Product: 0 producer put in 0 position: Product: 1 consumer removed from 0 Position: Product: 1 producer put in 0 position: Product: 2 producer put 1 position: Product: 3 consumer from 1 places: Products: 3 consumers take out 1 position: Product: 4 The consumer takes out from 0 position: Product: 2 producer put in 0 position: Product: 5 consumer removed from 0 Position: Product: 5 producer put in 0 position: Product: 6 consumer is removed from 0 position: Product: 6 producer put in 0 Location: Product: 7 producer puts 1 position: products £ º The producers are placed in the location of the product: 8 Position: Product Consumer removed from 1 position: Product: 9 consumer removed from 0 positions: Products: 7


Java multithreaded simulation producer consumer issues, corporate interview often asked questions ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.