用java实现管程,解决进程互斥问题
来源:互联网 发布:芜湖开淘宝企业店铺 编辑:程序博客网 时间:2024/06/03 19:16
import java.util.*;
public class ProduceConsumer{
static final int N = 2;//define the size of the buffer
static producer p = new producer();//initial a thread of producer
static consumer c = new consumer();//initial a thread of consumer
static our_monitor mon = new our_monitor();//initial a thread of a new pipe
public static void main(String args[]){
p.start();
c.start();
}
static class producer extends Thread{
public void run(){
int item;
while(true){//the loop of a producer
item = produce_item();
mon.insert(item);
}
}
private int produce_item(){
int p=1;
System.out.println("Produce:"+p);
return p;
}
}
static class consumer extends Thread{
public void run(){
int item;
while(true){
item = mon.remove();
consume_item(item);
}
}
private void consume_item(int item){
System.out.println("consumer:"+item);
}
}
static class our_monitor{//a pipe
private int buffer[] = new int[N];
private int count = 0,lo = 0, hi = 0;
public synchronized void insert(int val){
try{
Thread.sleep(500);
}catch(InterruptedException e){
System.out.println("error");
}
if(count == N)go_to_sleep();
buffer[hi] = val;
hi=(hi + 1) % N;
count = count + 1;
if(count == 1)notify();
}
public synchronized int remove(){
try{
Thread.sleep(500);
}catch(InterruptedException e){
System.out.println("error");
}
int val;
if(count == 0)go_to_sleep();
val = buffer[lo];
lo = (lo + 1) % N;
count = count -1;
if(count == N-1)notify();
return val;
}
private void go_to_sleep(){
try{
wait();
}catch(InterruptedException e){
System.out.println("error");
}
}
}
}
public class ProduceConsumer{
static final int N = 2;//define the size of the buffer
static producer p = new producer();//initial a thread of producer
static consumer c = new consumer();//initial a thread of consumer
static our_monitor mon = new our_monitor();//initial a thread of a new pipe
public static void main(String args[]){
p.start();
c.start();
}
static class producer extends Thread{
public void run(){
int item;
while(true){//the loop of a producer
item = produce_item();
mon.insert(item);
}
}
private int produce_item(){
int p=1;
System.out.println("Produce:"+p);
return p;
}
}
static class consumer extends Thread{
public void run(){
int item;
while(true){
item = mon.remove();
consume_item(item);
}
}
private void consume_item(int item){
System.out.println("consumer:"+item);
}
}
static class our_monitor{//a pipe
private int buffer[] = new int[N];
private int count = 0,lo = 0, hi = 0;
public synchronized void insert(int val){
try{
Thread.sleep(500);
}catch(InterruptedException e){
System.out.println("error");
}
if(count == N)go_to_sleep();
buffer[hi] = val;
hi=(hi + 1) % N;
count = count + 1;
if(count == 1)notify();
}
public synchronized int remove(){
try{
Thread.sleep(500);
}catch(InterruptedException e){
System.out.println("error");
}
int val;
if(count == 0)go_to_sleep();
val = buffer[lo];
lo = (lo + 1) % N;
count = count -1;
if(count == N-1)notify();
return val;
}
private void go_to_sleep(){
try{
wait();
}catch(InterruptedException e){
System.out.println("error");
}
}
}
}
- 用java实现管程,解决进程互斥问题
- 操作系统(四)同步互斥机制&管程&进程间通信
- 进程互斥问题
- 进程互斥问题
- 互斥与并发(3)—管程
- 互斥,同步,信号量,管程概述
- Windows 临界区 解决进程互斥问题
- 利用信号量机制解决进程同步和互斥问题
- Java进程间的同步与互斥实例(实现读者写者问题)
- 用PV操作解决进程之间的同步互斥问题
- linux网络编程之System V 信号量(二):用信号量实现进程互斥示例和解决哲学家就餐问题
- linux网络编程之System V 信号量(二):用信号量实现进程互斥示例和解决哲学家就餐问题
- java实现同步互斥问题
- 管程机制处理进程同步问题
- 管程机制处理进程同步问题
- 操作系统互斥进程问题
- 操作系统互斥进程问题
- windows实现进程互斥
- 【随机化贪心】【动态规划】【NOI2010】成长快乐
- 获取文件名的方式
- ORA-00257: archiver error. Connect internal only, until freed.
- JAVA线程池的原理及几类线程池的介绍
- 使用Struts1.2 Validator验证框架的简单例子
- 用java实现管程,解决进程互斥问题
- CSS居中的常用方法
- ubuntu下安装飞信
- iphone/iPad 搞定Audio Unit 2012/05/13
- s3c6410 LCD驱动的分析过程——定位相关源代码
- Tomcat配置出错问题一例
- python学习笔记,以此开始记录自己的python学习过程
- C++程序设计实验报告(七十一)---第十三周任务三
- 【动态规划】poj1952 buy low,buy lower