Java multithreading solves producer consumer problems _java

Source: Internet
Author: User

This article describes the Java multithreading solution to the problem of producer consumers. Share to everyone for your reference. The specific analysis is as follows:

The title is this:

Adopt Java Multithreading Technology, design and implement a program that conforms to the problem of producer and consumer. Operates on an object (the barrel) with a maximum capacity of 12 bullets. A producer thread is a thread that presses into the barrel of a bullet; a consumer thread is a shot through which it continuously shoots bullets from the barrel.

Requirements:

(1) Give the analysis process description.
(2) The output of the program should simulate the operation of pressing and ejection of the barrel;
(2) Design the program should take into account the synchronization of two threads.

This is almost the same as the famous producer consumer problem, and here's a simple analysis.

Just use the code to speak, the note is very clear

Package test;
Import java.util.ArrayList;
Import java.util.List;
 public class Testgun {public static void main (string[] args) {gunclip clip=new gunclip ();
 Producer p=new Producer (clip);
 Concumer c=new concumer (clip);
 P.start ();
 C.start ();  }/* First I want to have a cartridge, */class Gunclip//cartridge {Private list<integer> list=null;//used to put bullets private Boolean bfull =false ; It's important to understand a Boolean variable, which means full//function, like a bullet in a cartridge. Public synchronized void put (list list) {if (!bfull) {//meaning that the cartridge is not full, it's like a bullet in it.
  List=list; bfull=true;//filled with bullets, in the Boolean variable to true means that the cartridge is full notify ();//notice, waiting for the bullet thread to fetch the bullet}//The following try must wait when the cartridge is full and/or Boolean true
 , suspend this thread, wait for the bullet try{Waiting ();
 }catch (Exception e) {e.printstacktrace (); } public synchronized void get () {if (!bfull) {//If the cartridge is not full then you can't take the Bullet try{Wait ();/the cartridge is not full, so can not be, only waiting, only the cartridge is full to take the}catch (Ex
   Ception e) {e.printstacktrace ();
 The cartridge is full, then I start to take the bullet System.out.println ("\ n" + "machine gun begins to shoot bullets:" + "\ n"); for (int i=list.size (); i>0;i--) {int j= (Integer) List.get (I-1);
 System.out.println ("Shot" +j+ "bullet");
 //cartridge is emptied, that is, no bullets, then can only wait to refill,//So Boolean to False, indicating that the loaded thread loaded with bullets bfull=false;
 Notify ();
 }//producer, that is, loaded bullets, the following and bulletin board almost, to have a cartridge entity class Producer extends Thread {private Gunclip clip;
 Private list<integer> List;
 Producer (gunclip clip) {this.clip=clip;
  public void Run () {for (int i=0;i<3;i++) {//Let it produce 3 cartridges list=new ArrayList ();
  System.out.println ("\ n" + "machine gun began to press bullets:" + "\ n");
  for (int j=0;j<12;j++) {list.add (j+1);
  System.out.println ("has been pressed into the first" + (j+1) + "bullet");
  } clip.put (list);
 }}//consumer, fired bullets class Concumer extends Thread {private Gunclip clip;
 Concumer (gunclip clip) {this.clip=clip;
 public void Run () {while (true) {clip.get ();
 }
 }
}

I hope this article will help you with your Java programming.

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.