用JAVA多线程编程解决Lamport's Bakery问题
来源:互联网 发布:caffe 图像分割代码 编辑:程序博客网 时间:2024/05/22 11:53
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多线程编程解决Lamport's Bakery问题
- java--解决多线程问题
- Java多线程编程问题
- BAKERY'S ALGORITHM(面包店算法)
- java多线程编程注意问题
- 解决java编程乱码问题
- Bakery
- Bakery
- JAVA 多线程死锁问题及解决
- Java多线程解决生产者消费者问题
- Java多线程遇到的问题(待解决)
- 解决Java多线程并发的计数器问题
- 多线程死锁问题分析和解决[java]
- java多线程相关问题(待解决)
- Socket 多线程,Java C/S 模式 编程(服务器端)
- Socket 多线程,Java C/S 模式 编程(客户端)
- Java多线程编程— 线程同步问题
- 关于java 多线程编程的同步问题
- linux学习材料记录
- 如何进入linux 单用户模式维护
- C#特性学习与使用(为枚举定义Description)
- The Simplest App on MAC OSX
- 怎样使用Eclipse来开发Android源码
- 用JAVA多线程编程解决Lamport's Bakery问题
- IDA 封面人物 Ada Lovelace
- SQLserverAgent无法启动的解决方法
- 分享Silverlight/WPF/Windows Phone一周学习导读(10月16日-10月22日)
- implement a queue by using two stacks [No. 31]
- aspnetdb.mdf数据字典
- 横空出世,席卷互联网--评微软等公司数据结构和算法面试100题
- 11个帮你优化网站的 .htaccess 片段
- 80. Barking dogs seldom bite. 吠犬不咬人