生产者
import java.util.List; import java.util.Random; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; 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(); } } } public String getThreadName() { return threadName; } public void setThreadName(String threadName) { this.threadName = threadName; } public ReentrantLock getLock() { return lock; } public void setLock(ReentrantLock lock) { this.lock = lock; } public Condition getIsFull() { return isFull; } public void setIsFull(Condition isFull) { this.isFull = isFull; } public Condition getIsEmpty() { return isEmpty; } public void setIsEmpty(Condition isEmpty) { this.isEmpty = isEmpty; } public List<String> getTickets() { return tickets; } public void setTickets(List<String> tickets) { this.tickets = tickets; } }
消费者
import java.util.List; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; 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(); } } } public String getThreadName() { return threadName; } public void setThreadName(String threadName) { this.threadName = threadName; } public ReentrantLock getLock() { return lock; } public void setLock(ReentrantLock lock) { this.lock = lock; } public Condition getIsFull() { return isFull; } public void setIsFull(Condition isFull) { this.isFull = isFull; } public Condition getIsEmpty() { return isEmpty; } public void setIsEmpty(Condition isEmpty) { this.isEmpty = isEmpty; } public List<String> getTickets() { return tickets; } public void setTickets(List<String> tickets) { this.tickets = tickets; } }
测试类
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(); }); } }
标签:tickets,消费者,生产者,lock,isEmpty,isFull,手写,public,Condition From: https://www.cnblogs.com/use-D/p/17869184.html