Home > Java > javaTutorial > body text

Code Analysis for Consumer Issues in Java

不言
Release: 2018-09-11 14:05:41
Original
1773 people have browsed it

The content of this article is about code analysis of consumer issues in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Resource

public class Resource {
    //当前资源的数量
    int num = 0;
    //当前资源的上限
    int size = 10;

    //消费资源
    public synchronized void remove() {
        //如果num为0,没有资源了,需要等待
        while (num == 0) {
            try {
                System.out.println("消费者进入等待");
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果线程可以执行到这里,说明资源里有资源可以消费
        num--;
        System.out.println("消费者线程为:" + Thread.currentThread().getName() + "--资源数量:" + num);
        this.notifyAll();
    }

    //生产资源
    public synchronized void put() {
        //如果资源满了,就进入阻塞状态
        while (num == size) {
            try {
                System.out.println("生产者进入等待");
                this.wait();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        num++;
        System.out.println("生产者线程为:" + Thread.currentThread().getName() + "--资源数量:" + num);
        this.notifyAll();
    }
}
Copy after login

2. Consumer

public class Consumer implements Runnable {

    private Resource resource;

    public Consumer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (true){
            resource.remove();
        }

    }
}
Copy after login

3. Producer

public class Producer implements Runnable {

    private Resource resource;

    public Producer(Resource resource){
        this.resource=resource;
    }

    @Override
    public void run() {
        while (true){
            resource.put();
        }
    }
}
Copy after login

4. Test

public class TestConsumerAndProducer {

    public static void main(String[] args) {
        Resource resource = new Resource();
        //生产线程
        Producer p1 = new Producer(resource);
        //消费线程
        Consumer c1 = new Consumer(resource);

        new Thread(p1).start();

        new Thread(c1).start();
    }
}
Copy after login

Code Analysis for Consumer Issues in Java

Related recommendations:

Detailed explanation of examples of java producers and consumers

Java multi-threaded concurrent collaborative producer consumption Design pattern

The above is the detailed content of Code Analysis for Consumer Issues in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template