There are m customer threads and n salesperson threads. Each customer thread takes a rest for a random period of time, then enters the bakery and takes a number and waits (blocks) for the number to be called. Each salesperson thread serves the next waiting customer determined by number taken. Serving a customer takes a random amount of time. If there are no customers waiting to be served all salespersons go to sleep.
Write a solution to this problem.
问题解决.
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
class Bakery
{
Queue<Semaphore> customerqueue = new LinkedList<Semaphore>();//create queue for customer
Lock lock = new ReentrantLock(); //define lock
Condition bakeryStaffs = lock.newCondition();//define condition variable
public Bakery(){}
public void join(Semaphore sem)
{
//lock function
lock.lock();
try
{
customerqueue.add(sem); //The customer make a queue
}
finally
{
lock.unlock();
}
}
public void next()
{
lock.lock();
try
{
//If there no customer in bakery then the the staff will go to sleep
if(customerqueue.size() == 0)
{
try
{
bakeryStaffs.await();//Staff will sleep until customer call wake up
}
catch(InterruptedException e){}
}
//if customer queue size > 0 then staff will call customer come in bakery
if(customerqueue.size()>0)
{
//Create customer queue
//If customer have queue, than there will one by one enter bakery
Semaphore s = customerqueue.poll();
s.release();
}
}
finally
{
lock.unlock();
}
}
//Call bakery staff wake up
public void call()
{
lock.lock();
//bakery staff call customer
try
{
bakeryStaffs.signalAll();
}
finally
{
lock.unlock();
}
}
}
class SalePerson extends Thread
{
//Create variables
Bakery hotbakery;
int staffNumber;
//Constructors
public SalePerson(Bakery hb, int n)
{
hotbakery = hb;
staffNumber = n;
}
public void run()
{
while(true)
{
//Bakery staff waiting for customer
System.out.println(staffNumber + " Bakery Staff: I hava a rest");
try
{
Thread.sleep(2000);
}
catch(InterruptedException e){}
//Bakery staff call next customer
System.out.println(staffNumber + " Bakery staff: next customer!");
hotbakery.next();
}
}
}
class Customer extends Thread
{
//variables
Semaphore sem = new Semaphore(0);
Bakery customer;
int customerNumber;
//Constructors
public Customer(Bakery c, int cn)
{
customer = c;
customerNumber = cn;
}
public void run()
{
//Customer join queue
customer.join(sem);
try
{
//Create random waiting time
int t = (int)(Math.random()*10000);
Thread.sleep(t);
System.out.println(customerNumber + " Customer: I'm take a break!");
//customer call staff wake up
customer.call();
System.out.println(customerNumber + " Customer: I go to bakery now!");;
//customer leaving
sem.acquire();
System.out.println(customerNumber + " Customer: I'm leaving");
}
catch(InterruptedException e){}
}
}
class BakeryTest
{
public static void main(String args[])
{
Bakery bakery = new Bakery();
//implement bakery staff threads
for(int i=0; i<4; i++)
{
new SalePerson(bakery,i).start();
}
//implement customer threads
for(int i=0; i<10; i++)
{
new Customer(bakery,i).start();
}
//create customer wait enter bakery time
for(int j=0; j<10; j++)
{
try
{
Thread.sleep(3000);
}catch(InterruptedException e){}
}
}
}
本文介绍了一个使用Java实现的烘焙店场景:多个顾客线程与销售人员线程通过信号量和条件变量进行同步操作。顾客线程随机休息一段时间后进入烘焙店排队等待服务,销售人员线程则按顾客到达的顺序提供服务。
8065

被折叠的 条评论
为什么被折叠?



