/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javapro_con;
/**
*
* @author chun
*/
public class P_C {
public static void main(String[] args){
Holdint h=new Holdint(); //h为监控器
Producer p=new Producer(h);
Consumer c=new Consumer(h);
p.start();
c.start();
}
}
class Holdint{
private int sharedint;
private boolean writeable=true;//生产者线程能生产新数据
public synchronized void set(int value){//临界区
while(!writeable)
{//生产者不能生产数据时等待
try{wait();}
catch(InterruptedException e){}
}//生产者被唤醒后继续执行
writeable=false;
sharedint=value;
notify();
}
public synchronized int get(){
while(writeable){//消费者不能消费时等待
try{wait();}
catch(InterruptedException e){}
}//生产者被唤醒后继续执行
writeable=true;
notify();
return sharedint;
}
}
//
class Producer extends Thread{
private Holdint hi;
// private Object s;
public Producer(Holdint hiform)
{hi=hiform;}
@Override
public void run(){
for(int i=0;i<=9;i++){
int n = (int) (Math.random()*10);
hi.set(n);
System.out.println("生产者生产:"+n);
}
}
}
class Consumer extends Thread{
private Holdint hi;
public Consumer(Holdint hiform)
{hi=hiform;}
@Override
public void run(){
for(int i=0;i<=9;i++){
int value=hi.get();
System.out.println("消费者消费:"+value);
try{
Thread.sleep(100);
}
catch(InterruptedException e){}
}
}
}
求解和为15的棋盘游戏问题和为15的棋盘游戏
5星 · 超过95%的资源 需积分: 12 69 浏览量
2011-05-06
16:57:48
上传
评论 1
收藏 14KB RAR 举报
csrych
- 粉丝: 0
- 资源: 7