import java.util.*;
import java.util.concurrent.TimeUnit;
import java.text.SimpleDateFormat;
//---------------------------------------------------------------
public class Hamburger
{
static Queue que = new LinkedList();
static semaphore sem = new semaphore(0);
static semaphore mutex = new semaphore(1);
//----------------------------------------------------------------
static class Producer extends Thread
{ String namep;
long startTimep;
public Producer(String name)
{
this.namep = name;
startTimep = System.currentTimeMillis();
}
public void run()
{
try
{
long start = System.currentTimeMillis();
long end = start + 100*1000;// simulated for 30 seconds. Produce for ...seconds
int a = 1;
while (System.currentTimeMillis() < end)
{
//TimeUnit.SECONDS.sleep(1);
mutex.waits();
a=a+1;//new Integer(N);
que.add(a);
long estimatedTime = System.currentTimeMillis() - startTimep;
System.out.println("producer: "+namep+" Produces "+que.peek()+" at the time: "+estimatedTime);
mutex.signal();
sem.signal();
Thread.sleep(1000);
}
}
catch (Exception x)
{
x.printStackTrace();
}
}
}
//-----------------------------------------------------------------------------------------------------------
static class Consumer extends Thread
{
long startTime;
String namec;
long servicetime;
int count=1;
public int eat(Object obj)
{
return (this.count++);
}
public Consumer(String name)
{ //eat=0;
this.namec = name;
startTime = System.currentTimeMillis();
}
public void run()
{
try
{
while (true)
{
sem.waits();
mutex.waits();
long estimatedTime = System.currentTimeMillis() - startTime;
System.out.print("Consumer: "+ namec+" eats "+ que.poll()+" at the time: "+estimatedTime);
System.out.println(" Totall Hamburgers: "+ eat(this.namec));
//TimeUnit.SECONDS.sleep(1);
mutex.signal();
Thread.sleep(1000);
}
}
catch (Exception x)
{
x.printStackTrace();
}
}
}
//----------------------------------------------------------------------------------------------------
public static void main(String [] args)
{
System.out.print("This program Simulated Running Producer/Consumer problem\nWith two producers and five consumers\nfor 100 seconds\n\n");
System.out.println("Enter '1' and press Enter key to continue...");
Scanner scan = new Scanner(System.in);
String s = scan.next();
new Producer("prod1 ").start();
new Producer("prod2 ").start();
new Consumer("Ali ").start();
new Consumer("Mohsen ").start();
new Consumer("Milad ").start();
new Consumer("Danial ").start();
}
}
Os_project_1.rar_MPI
版权申诉
57 浏览量
2022-09-20
23:36:45
上传
评论
收藏 6KB RAR 举报
APei
- 粉丝: 64
- 资源: 1万+
最新资源
- face-detect.ipynb
- 以下是一些关于ACM(国际大学生程序设计竞赛)、NOI(全国青少年信息学奥林匹克竞赛)以及CSP(全国青少年信息学奥林匹克竞赛提
- 是一些电子设计竞赛(电赛)经验分享,包括备赛策略、项目管理、团队合作和比赛期间的注意事项
- 全能运行库修复工具DirectX Repair v4.1.0.30770
- las格式点云数据使用详解(附VS编译好的LAStools工具)
- KRPano插件一键解密大师1.4.0 (解压密码1234)
- 适用于618节日的销售数据
- 《C++沉思录》是一本由 Scoot Meyers 所著的经典 C++ 编程书籍 该书深入探讨了 C++ 的一些高级概念和技术
- 海信刷机数据 LED42K310X3D(0000)BOM1-C006软件数据与LED42K310NX3D(0000)BOM1通用
- 网卡硬刷MAC地址的工具和教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈