用java实现管程,解决进程互斥问题
2012-05-13 09:25
429 查看
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
;
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
;
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实现,可推广解决N皇后问题
- java实现数据库备份和恢复源码---解决路径带空格引发的问题
- java 实现新浪微博内容计数器 Java问题通用解决代码
- 一个简单的Java对象池实现——可用来解决SimpleDateFormat的线程安全问题
- java实现插入mysql二进制文件,blob类型,遇到问题及解决办法
- 营救公主(Java实现A*算法解决迷宫问题)
- Java 实现Ftp乱码问题解决
- 广度优先解决一维坐标移动问题java实现
- 数三退一的问题解决(C语言和Java实现)
- Java实现ftp文件上传下载解决慢中文乱码多个文件下载等问题
- 动态规划解决01背包问题(java实现)
- java 自己实现项目一键全转码 解决文件乱码问题
- Java语言解决约瑟夫环问题(链表实现)
- java基于拉链法实现的map,解决地址冲突的问题
- java实现中文文件下载,解决乱码问题,亲测无问题~~
- JAVA_解决实现接口方法重名问题
- Java实现Zip压缩与解压(解决中文乱码问题)
- mybatis和JPA实现乐观锁解决并发问题-阿里巴巴JAVA开发手册详细解读
- (转)javabean操作文件正确,但是Jsp调用javabean时文件路径出错问题解决之JavaBean访问本地文件实现路径无关实现方法
- 在Java中实现.net中DataTable功能以及操作双数据库的List连接问题解决方案探究