Producer Consumer с семафором_ Ошибка создания - PullRequest
0 голосов
/ 11 апреля 2020

Я реализовал приложение производителя-потребителя, которое предполагает произвести 50 элементов, а затем потреблять эти элементы.

После запуска следующей программы приложение создает только один элемент и ничего не потребляет.

Журнал консоли: neslysoft_multithreading "pc_Semaphore.SemaphoreMain Добавлено: 0

Основной класс

package pc_Semaphore;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreMain {

    public static void main(String[] args) {

        List<Integer> arrayList = new LinkedList<>();
        int capacity = 50;

        Semaphore semaphoreProducer = new Semaphore(1);
        Semaphore semaphoreConsumer = new Semaphore(0);

        Producer producer = new Producer(arrayList, capacity, semaphoreProducer, semaphoreConsumer);
        Consumer consumer = new Consumer(arrayList, capacity, semaphoreConsumer, semaphoreConsumer);

        Thread t1 = new Thread(producer);
        Thread t2 = new Thread(consumer);

        ExecutorService es = Executors.newSingleThreadExecutor();
        es.submit(t1);
        es.submit(t2);

        es.shutdown();
        try {
            es.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {}

    }
}

Класс производства

package pc_Semaphore;

import java.util.List;
import java.util.concurrent.Semaphore;

public class Producer implements Runnable{

    List<Integer> linkedList;
    Semaphore semaphoreProducer;
    Semaphore semaphoreConsumer;
    int capacity;


    public Producer(List linkedList, int capacity, Semaphore semaphoreProducer, Semaphore semaphoreConsumer){
        this.linkedList = linkedList;
        this.capacity = capacity;
        this.semaphoreProducer = semaphoreProducer;
        this.semaphoreConsumer = semaphoreConsumer;
    }

    @Override
    public void run() {

        for(int i=0;i<50;i++){

            try{
                semaphoreProducer.acquire();
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            linkedList.add(i);
            System.out.println("Added: " + i);
            semaphoreConsumer.release();
        }
    }
}

Класс потребления

package pc_Semaphore;

import java.util.List;
import java.util.concurrent.Semaphore;

public class Consumer implements  Runnable{

    List<Integer> linkedList;
    Semaphore semaphoreProducer;
    Semaphore semaphoreConsumer;

    public Consumer(List linkedList, int capacity, Semaphore semaphoreProducer, Semaphore semaphoreConsumer){
        this.linkedList = linkedList;
        this.semaphoreProducer = semaphoreProducer;
        this.semaphoreConsumer = semaphoreConsumer;
    }

    @Override
    public void run() {
        while(true){
            try{
                semaphoreConsumer.acquire();
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("Removed : " + linkedList.remove(0));
            semaphoreProducer.release();
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...