Code Analysis of consumer problems in Java

Source: Internet
Author: User
This article gives you the content is about the Java Consumer Problem Code analysis, there is a certain reference value, the need for a friend can refer to, I hope you have some help.

1. Resources

public class Resource {//The number of current resources int num = 0;    Upper limit of current resource int size = 10;                Consumer resources public synchronized void remove () {//If num is 0, there is no resource, you need to wait while (num = 0) {try {                SYSTEM.OUT.PRINTLN ("Consumer enters Wait");            This.wait ();            } catch (Interruptedexception e) {e.printstacktrace ();        }}//If the thread can execute here, it indicates that resources have resources to consume num--;        SYSTEM.OUT.PRINTLN ("Consumer thread:" + thread.currentthread (). GetName () + "--Number of resources:" + num);    This.notifyall ();                }//Production resource public synchronized void put () {//If the resource is full, it enters the blocking state while (num = = size) {try {                System.out.println ("producer enters Wait");            This.wait ();            } catch (Interruptedexception e) {e.printstacktrace ();        }} num++;        System.out.println ("Producer Thread:" + thread.currentthread (). GetName () + "--Number of resources:" + num);    This.notifyall (); }}

2. Consumers

public class Consumer implements Runnable {    private Resource Resource;    Public Consumer (Resource Resource) {        this.resource = Resource;    }    @Override public    Void Run () {        while (true) {            resource.remove ()}        }    }

3. Producers

public class Producer implements Runnable {    private Resource Resource;    Public Producer (Resource Resource) {        this.resource=resource;    }    @Override public    Void Run () {        while (true) {            resource.put ()}        }    }

4. Testing

public class Testconsumerandproducer {public    static void Main (string[] args) {        Resource Resource = new Resource () ;        Production line        Producer p1 = new Producer (resource);        Consumer thread        Consumer c1 = new Consumer (resource);        New Thread (p1). Start ();        New Thread (C1). Start ();    }}

Related Article

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.