第二个线程不生产消费的例子开始(Second thread doesn't start in

2019-10-30 11:53发布

我试图通过使用来实现生产者和消费者问题的semaphores在java中。 问题是,当我开始两个线程(生产者和消费者)的消费者不启动和制片块本身缓冲区已满后。 我的意思是,它看起来像只有一个线程这在同步的方式工作。 因此,正如我所提到我使用3个信号量,其是空的,满,和互斥。 这是最简单的代码;

制作类;

import java.util.concurrent.Semaphore;

public class Producer implements Runnable {

    private Semaphore empty;
    private Semaphore full;
    private Semaphore mutex;

    public Producer(Semaphore empty, Semaphore full, Semaphore mutex) {

        this.empty = empty;
        this.full = full;
        this.mutex = mutex;
    }

    @Override
    public void run() {

        while (true) {

            try {
                empty.acquire();
                mutex.acquire();
                Thread.sleep(500);
                System.out.println("Producer producess an element");
                mutex.release();
                full.release();
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
        }
    }
}

消费类;

import java.util.concurrent.Semaphore;

public class Consumer implements Runnable {

    private Semaphore empty;
    private Semaphore full;
    private Semaphore mutex;

    public Consumer(Semaphore empty, Semaphore full, Semaphore mutex) {

        this.empty = empty;
        this.full = full;
        this.mutex = mutex;
    }

    @Override
    public void run() {

        while (true) {

            try {
                full.acquire();
                mutex.acquire();
                Thread.sleep(500);
                System.out.println("Consumer consumes an element");
                mutex.release();
                empty.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

ProducerConsumerExample类

import java.util.concurrent.Semaphore;

public class ProducerConsumerProblem {

    private Semaphore empty;
    private Semaphore full;
    private Semaphore mutex;

    public ProducerConsumerProblem(int empty, int full) {

        this.empty = new Semaphore(empty);
        this.full = new Semaphore(full);
        this.mutex = new Semaphore(1);

    }

    public void runProducerConsumerExample() {

        Producer producer = new Producer(empty, full, mutex);
        Consumer consumer = new Consumer(empty, full, mutex);

        Thread p = new Thread(producer);
        Thread c = new Thread(consumer);

        p.run();
        c.run();
    }
}

最后测试类

import org.junit.Before;
import org.junit.Test;

public class ProducerConsumerProblemTest {

    private ProducerConsumerProblem testClass;
    private static final int        EMPTY = 10;
    private static final int        FULL  = 0;

    @Before
    public void setUp() {

        testClass = new ProducerConsumerProblem(EMPTY, FULL);
    }

    @Test
    public void testName() {

        testClass.runProducerConsumerExample();
    }
}

输出:

Producer producess an element
Producer producess an element
Producer producess an element
Producer producess an element
Producer producess an element
Producer producess an element
Producer producess an element
Producer producess an element
Producer producess an element
Producer producess an element

经过10项生产没有任何反应和线程被阻塞。

Answer 1:

不要用run() ,使用start()

    p.run();
    c.run();

应该

    p.start();
    c.start();

调用run()不产生新的执行线程。 它只是运行的执行当前线程中的功能。

见这个问题/答案的更多细节。

还要注意,我会关闭线程在正确的测试运行结束(否则你会为你添加/运行测试,建立多线程)。 中断你的线程或设置一个布尔值波动,表明你的循环应该完成。



文章来源: Second thread doesn't start in producer consumer example