请教多线程的问题,麻烦哪位高手帮忙,谢谢

zapdos 2007-06-15 02:54:01
不知为什么,在一个noify里面会卡住,请各位帮帮忙,谢谢

class test{
java.util.Random r = new java.util.Random();
老板 老板a = new 老板();
员工 员工a = new 员工();
老婆 老婆a = new 老婆();
妹妹 妹妹a = new 妹妹();
孙女 孙女a = new 孙女();
class 没有资料 extends Exception{}
class 怀孕 extends Exception{}
class 有事 extends Exception{}
class 不用学习 extends Exception{}
class 跟孙女玩 extends Exception{}
class 人物 extends Thread{
资料 资料a = null;
String 状态 = null;
public void 得到资料(资料 资料a){
this.资料a = 资料a;
}
public void 失去资料(){
this.资料a = null;
}
public void 递资料(人物 人物a){
System.out.println(人物a+"拿到资料");
失去资料();
人物a.得到资料(资料a);
}
public void 通知(人物 人物a,String 状态){
try{
this.sleep(r.nextInt(300));
}catch(Exception e){}
人物a.改变状态(状态);
synchronized(人物a){
人物a.notify();
}
}
public void 改变状态(String 状态){
this.状态 = 状态;
}
}
class 资料{
public String toString(){
return "资料上面写着:不得随地大小便!";
}
}
class 老板 extends 人物{
public 老板(){
状态 = "工作";
start();
}
public void run(){
while(true){
try{
synchronized(this){
wait();
}
}catch(Exception e){}
if(状态.equals("工作"))
看资料();
else
玩();
}
}
public void 看资料(){
try{
if(资料a==null) throw new 没有资料();
System.out.println("老板正在看资料....\n"+资料a);
System.exit(0);
}catch(没有资料 e){
通知(员工a,"上交资料");
}
}
public void 玩(){
System.out.println("老板要玩");
if(资料a!=null){
递资料(员工a);
}
//System.out.println(员工a);
通知(员工a,"取消上交资料");//就是这里卡住了
}
public String toString(){
return "老板";
}
}
class 员工 extends 人物{
public 员工(){
start();
}
public void run(){
while(true){
try{
synchronized(this){
wait(100);
}
}catch(Exception e){}
if(状态.equals("上交资料"))
上交资料();
else
取消上交资料();
}
}
public void 上交资料(){
try{
if(资料a==null) throw new 没有资料();
递资料(老板a);
synchronized(老板a){
老板a.notify();
}
}catch(没有资料 e){
通知(老婆a,"送资料");
上交资料();
}
}
public void 取消上交资料(){
if(资料a!=null){
递资料(妹妹a);
}
通知(老婆a,"取消送资料");
}
public String toString(){
return "员工";
}
}
class 老婆 extends 人物{
public 老婆(){
资料a = new 资料();
start();
}
public void run(){
while(true){
try{
synchronized(this){
wait();
}
}catch(Exception e){}
if(状态.equals("送资料"))
送资料();
else
取消送资料();
}
}
public void 送资料(){
try{
if(true) throw new 怀孕();
递资料(员工a);
synchronized(员工a){
员工a.notify();
}
}catch(怀孕 e){
递资料(妹妹a);
通知(妹妹a,"送资料");
}
}
public void 取消送资料(){
通知(妹妹a,"取消送资料");
}
public String toString(){
return "老婆";
}
}
class 妹妹 extends 人物{
public 妹妹(){
start();
}
public void run(){
while(true){
try{
synchronized(this){
wait();
}
}catch(Exception e){}
if(状态.equals("送资料"))
送资料();
else
取消送资料();
}
}
public void 送资料(){
try{
if(孙女a.状态.equals("学习")) throw new 有事();
递资料(员工a);
}catch(有事 e){
通知(孙女a,"玩");
送资料();
}
}
public void 取消送资料(){
if(资料a!=null){
递资料(老婆a);
}
通知(孙女a,"学习");
}
public String toString(){
return "妹妹";
}
}
class 孙女 extends 人物{
public 孙女(){
状态 = "学习";
start();
}
public void run(){
while(true){
try{
synchronized(this){
wait();
}
}catch(Exception e){}
if(状态.equals("玩"))
玩();
else
学习();
}
}
public void 玩(){
System.out.println("孙女玩");
通知(老板a,"玩");
}
public void 学习(){
System.out.println("孙女学习");
通知(老板a,"工作");
}
public String toString(){
return "孙女";
}
}
public static void main(String args[]){
test t = new test();
t.老板a.看资料();
}
}
...全文
202 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tommy___2005 2007-06-18
  • 打赏
  • 举报
回复
public void 玩() {
System.out.println("老板要玩");
if (资料a != null) {
递资料(员工a);
}
通知(员工a, "取消上交资料");//这里我已经notify员工了,但是为什么员工还处于wait状态呢(run里面wait以下的内容并没有执行)?难道是出现死锁了?
}
这个函数从来就没有执行,因为你没有启动老板线程
zapdos 2007-06-15
  • 打赏
  • 举报
回复
注释里面写着问题,请帮忙,谢谢
zapdos 2007-06-15
  • 打赏
  • 举报
回复
就是这个:

class test{
java.util.Random r = new java.util.Random();
老板 老板a = new 老板();
员工 员工a = new 员工();
老婆 老婆a = new 老婆();
妹妹 妹妹a = new 妹妹();
孙女 孙女a = new 孙女();
class 没有资料 extends Exception{}
class 怀孕 extends Exception{}
class 有事 extends Exception{}
class 不用学习 extends Exception{}
class 跟孙女玩 extends Exception{}
class 人物 extends Thread{
资料 资料a = null;
String 状态 = null;
public void 得到资料(资料 资料a){
this.资料a = 资料a;
}
public void 失去资料(){
this.资料a = null;
}
public void 递资料(人物 人物a){
System.out.println(人物a+"拿到资料");
失去资料();
人物a.得到资料(资料a);
}
public void 通知(人物 人物a,String 状态){
try{
this.sleep(r.nextInt(300));
}catch(Exception e){}
人物a.改变状态(状态);
synchronized(人物a){
人物a.notify();
}
}
public void 改变状态(String 状态){
this.状态 = 状态;
}
}
class 资料{
public String toString(){
return "资料上面写着:不得随地大小便!";
}
}
class 老板 extends 人物{
public 老板(){
状态 = "工作";
}
public void run(){
看资料();
while(true){
try{
synchronized(this){
wait();
}
}catch(Exception e){}
if(状态.equals("工作"))
看资料();
else
玩();
}
}
public void 看资料(){
try{
if(资料a==null) throw new 没有资料();
System.out.println("老板正在看资料....\n"+资料a);
System.exit(0);
}catch(没有资料 e){
通知(员工a,"上交资料");
}
}
public void 玩(){
System.out.println("老板要玩");
if(资料a!=null){
递资料(员工a);
}
通知(员工a,"取消上交资料");//这里我已经notify员工了,但是为什么员工还处于wait状态呢(run里面wait以下的内容并没有执行)?难道是出现死锁了?
}
public String toString(){
return "老板";
}
}
class 员工 extends 人物{
public 员工(){
start();
}
public void run(){
while(true){
try{
synchronized(this){
wait();
}
}catch(Exception e){}
if(状态.equals("上交资料"))
上交资料();
else
取消上交资料();
}
}
public void 上交资料(){
try{
if(资料a==null) throw new 没有资料();
递资料(老板a);
synchronized(老板a){
老板a.notify();
}
}catch(没有资料 e){
通知(老婆a,"送资料");
上交资料();
}
}
public void 取消上交资料(){
if(资料a!=null){
递资料(妹妹a);
}
通知(老婆a,"取消送资料");
}
public String toString(){
return "员工";
}
}
class 老婆 extends 人物{
public 老婆(){
资料a = new 资料();
start();
}
public void run(){
while(true){
try{
synchronized(this){
wait();
}
}catch(Exception e){}
if(状态.equals("送资料"))
送资料();
else
取消送资料();
}
}
public void 送资料(){
try{
if(true) throw new 怀孕();
递资料(员工a);
synchronized(员工a){
员工a.notify();
}
}catch(怀孕 e){
递资料(妹妹a);
通知(妹妹a,"送资料");
}
}
public void 取消送资料(){
通知(妹妹a,"取消送资料");
}
public String toString(){
return "老婆";
}
}
class 妹妹 extends 人物{
public 妹妹(){
start();
}
public void run(){
while(true){
try{
synchronized(this){
wait();
}
}catch(Exception e){}
if(状态.equals("送资料"))
送资料();
else
取消送资料();
}
}
public void 送资料(){
try{
if(孙女a.状态.equals("学习")) throw new 有事();
递资料(员工a);
}catch(有事 e){
通知(孙女a,"玩");
送资料();
}
}
public void 取消送资料(){
if(资料a!=null){
递资料(老婆a);
}
通知(孙女a,"学习");
}
public String toString(){
return "妹妹";
}
}
class 孙女 extends 人物{
public 孙女(){
状态 = "学习";
start();
}
public void run(){
while(true){
try{
synchronized(this){
wait();
}
}catch(Exception e){}
if(状态.equals("玩"))
玩();
else
学习();
}
}
public void 玩(){
System.out.println("孙女玩");
通知(老板a,"玩");
}
public void 学习(){
System.out.println("孙女学习");
通知(老板a,"工作");
}
public String toString(){
return "孙女";
}
}
public static void main(String args[]){
test t = new test();
t.老板a.start();
}
}

请指教,谢谢
gefengxztg 2007-06-15
  • 打赏
  • 举报
回复
String 状态 = null; 改为 String 状态 = ""; 编译没问题
不知道你要问什么?

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧