| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 547 人关注过本帖
标题:[求助]java多线程编程
只看楼主 加入收藏
zhaimin889
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2006-12-28
收藏
 问题点数:0 回复次数:3 
[求助]java多线程编程
问题:有四个进程,分别是farther,mother,son,daughter;有一个盘子
若farther获得盘子就在其上放苹果,若mother获得盘子就在其上放梨,
son线程取苹果,daughter取梨.
用java实现(最好有完整代码)
搜索更多相关主题的帖子: java 线程 
2007-04-24 23:44
sunspirit
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-4-10
收藏
得分:0 

import java.io.*;
import java.lang.Thread;
import java.lang.String;
import java.lang.Math;

class Plate{
boolean apple,pear;
public Plate(){
apple = false;
pear = false;
}

public synchronized void getApple(){
if(apple)
System.out.println(" get an apple from the plate!");
else
System.out.println(" fail to get an apple ,beacuse there is no apple !");
apple = false;
}

public synchronized void putApple(){
if(!apple)
System.out.println(" put an apple to the plate!");
else
System.out.println(" fail to put an apple,because there is an apple on the plate already!");
apple = true;
}

public synchronized void getPear(){
if(pear)
System.out.println(" get an pear from the plate!");
else
System.out.println(" fail to get an pear ,beacuse there is no pear !");
pear = false;
}

public synchronized void putPear(){
if(!pear)
System.out.println(" put an pear to the plate!");
else
System.out.println(" fail to put an pear,because there is an pear on the plate already!");
pear = true;
}
}

class Person extends Thread{
String name ;
Plate plate;
public Person(Plate plate){
this.plate = plate;
}

public void action(){};
public void run(){
for(int i = 0;i<10;i++){
getTheName();
action();
double time = Math.random()*1000;
try{
this.sleep((long)time);
}catch(Exception e){
}

}

}
public void getTheName(){
System.out.print(name);
}
}

class Mother extends Person{
public Mother(Plate plate){
super(plate);
name = "Mother";
}

public void action(){
this.plate.putPear();
}

}

class Father extends Person{
public Father(Plate plate){
super(plate);
name = "Father";
}

public void action(){
this.plate.putApple();
}
}

class Son extends Person{
public Son(Plate plate){
super(plate);
name = "Son";
}

public void action(){
this.plate.getApple();
}
}

class Daughter extends Person{
public Daughter(Plate plate){
super(plate);
name = "Daugther";
}

public void action(){
this.plate.getPear();
}
}

public class MThread{
public static void main(String [] args){
Plate plate = new Plate();
Person father = new Father(plate);
Person mother = new Mother(plate);
Person son = new Son(plate);
Person daughter = new Daughter(plate);

father.start();
mother.start();
son.start();
daughter.start();
}
}

2007-04-25 10:41
hp8526
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-3-24
收藏
得分:0 
2007-04-25 13:24
zhaimin889
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2006-12-28
收藏
得分:0 

先表示谢谢!!!!
拿回去研究研究。
若有不懂之处,再劳烦大虾
谢谢!!


2007-04-25 20:56
快速回复:[求助]java多线程编程
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015876 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved