消费者与生产者

生产者

import lombok.Data;

import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

@Data
public class ConsumerThread implements Runnable {

    private String threadName;

    private ReentrantLock lock;

    private Condition isFull;

    private Condition isEmpty;

    private List<String> tickets;

    @Override
    public void run() {
        while (true) {
            if (!lock.tryLock()) {
                continue;
            }

            try {
                if (tickets.size() == 0) {
                    isEmpty.await();
                    continue;
                }
                int index = tickets.size() - 1;
                String ticketName = tickets.get(index);
                tickets.remove(index);
                System.out.println("[" + Thread.currentThread().getName() + "]消费:" + ticketName);
                isFull.signalAll();
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}

 

消费者

import lombok.Data;

import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

@Data
public class ProducerThread implements Runnable {

    private String threadName;

    private ReentrantLock lock;

    private Condition isFull;

    private Condition isEmpty;

    private List<String> tickets;
    @Override
    public void run() {
        while (true) {
            if (!lock.tryLock()) {
                continue;
            }

            try {
                if (tickets.size() >= 10) {
                    isFull.await();
                    continue;
                }
                Random random = new Random();

                String ticketName = "门票" + random.nextInt(10);
                tickets.add(ticketName);
                System.out.println("[" + Thread.currentThread().getName() + "]生产:" + ticketName);
                isEmpty.signalAll();
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}

 

测试类

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class TicketMain {

    static List<ProducerThread> producerThreads = new ArrayList<>();
    static List<ConsumerThread> consumerThreads = new ArrayList<>();

    static ReentrantLock lock = new ReentrantLock();
    static Condition isFull = lock.newCondition();
    static Condition isEmpty = lock.newCondition();
    static volatile List<String> tickets = new ArrayList<>();

    static {
        for (int i = 0; i < 10; i++) {
            ProducerThread producerThread = new ProducerThread();
            producerThread.setThreadName("生产者" + i);
            producerThread.setLock(lock);
            producerThread.setIsEmpty(isEmpty);
            producerThread.setIsFull(isFull);
            producerThread.setTickets(tickets);
            producerThreads.add(producerThread);
        }
        for (int i = 0; i < 10; i++) {
            ConsumerThread consumerThread = new ConsumerThread();
            consumerThread.setThreadName("消费者" + i);
            consumerThread.setLock(lock);
            consumerThread.setIsEmpty(isEmpty);
            consumerThread.setIsFull(isFull);
            consumerThread.setTickets(tickets);
            consumerThreads.add(consumerThread);
        }

    }

    public static void main(String[] args) {
        producerThreads.stream().forEach(e->{
            Thread thread = new Thread(e);
            thread.setName(e.getThreadName());
            thread.start();
        });
        consumerThreads.stream().forEach(e->{
            Thread thread = new Thread(e);
            thread.setName(e.getThreadName());
            thread.start();
        });
    }
}

 

posted @ 2024-05-06 22:29  使用D  阅读(1)  评论(0编辑  收藏  举报