| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1273 人关注过本帖
标题:有关线程程序的错误
只看楼主 加入收藏
syuanq
Rank: 2
等 级:新手上路
威 望:3
帖 子:297
专家分:0
注 册:2006-12-11
结帖率:0
收藏
 问题点数:0 回复次数:12 
有关线程程序的错误

class A implements Runnable
{
B a=new B();
Thread zhangsan=new Thread(this);
Thread lisi=new Thread(this);
Thread wangwu=new Thread(this);
zhangsan.start();
lisi.start();
wangwu.start();
public void run()
{
if(currentThread()==zhangsan)
{
a.course(20);
}
if(currentThread()==lisi)
{
a.course(10);
}
if(currentThread()==wangwu)
{
a.course(5);
}
}
}
class B
{
int money5=3,money10=0,money20=0;
public void course(int money)
{
if(money==5)
{
money5=money5+1;
System.out.println("你给的钱刚好");
}
if(money==10)
{
while(money5<1)
{
try
{
wait();
}
catch(InterruptedException e){
}
money5=money5-1;
money10=money10+1;
System.out.println("你给我10元,我找你5元");
}
}
if(money=20)
{
while(money5<3)
{
try
{
wait();
}
catch(InterruptedException e){
}
money5=money5-3;
money20=money20+1;
System.out.println("你给我20元,我找你15元");
}
}
notifyAll();
}
}



--------------------Configuration: <Default>--------------------
F:\java\syjc\ThreadTest.java:312: 需要 <标识符>
zhangsan.start();
^
F:\java\syjc\ThreadTest.java:313: 需要 <标识符>
lisi.start();
^
F:\java\syjc\ThreadTest.java:314: 需要 <标识符>
wangwu.start();
^
3 错误

Process completed.
但是不知道是什么错?谢谢指点

搜索更多相关主题的帖子: 线程 Thread course new 
2007-10-24 13:41
heilong
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:777
专家分:0
注 册:2007-6-7
收藏
得分:0 
zhangsan.start();
lisi.start();
wangwu.start();
最好是把这几个放在RUN后面。thread(this)里的this,不要。

风水鸡蛋壳,财去人安乐!----->
2007-10-25 01:25
syuanq
Rank: 2
等 级:新手上路
威 望:3
帖 子:297
专家分:0
注 册:2006-12-11
收藏
得分:0 
换了还是不行呢?this表示实现Runnable接口的实例啊

[url]www.[/url]欢迎大家的光临,一起交流学习
2007-10-25 12:49
HK狼
Rank: 1
等 级:新手上路
威 望:1
帖 子:46
专家分:0
注 册:2007-9-9
收藏
得分:0 
this没有错。。

你的程序连main方法的没有?
不是随便把zhangsan.start();
lisi.start();
wangwu.start();
放在任意地方就可以运行的!

程序总得有个入口吧!


2007-10-25 18:53
syuanq
Rank: 2
等 级:新手上路
威 望:3
帖 子:297
专家分:0
注 册:2006-12-11
收藏
得分:0 
加了main方法后仍然报错哦。。。报错如下:
--------------------Configuration: <Default>--------------------
F:\java\syjc\ThreadTest.java:317: 非法的表达式开始
public void run()
^
F:\java\syjc\ThreadTest.java:332: 需要 ';'

^
2 错误

[url]www.[/url]欢迎大家的光临,一起交流学习
2007-10-25 23:16
syuanq
Rank: 2
等 级:新手上路
威 望:3
帖 子:297
专家分:0
注 册:2006-12-11
收藏
得分:0 
一直找不出到底哪里出错了

[url]www.[/url]欢迎大家的光临,一起交流学习
2007-10-25 23:16
george_vcool
Rank: 2
等 级:新手上路
威 望:3
帖 子:453
专家分:0
注 册:2007-7-23
收藏
得分:0 

你的
zhangsan.start();
lisi.start();
wangwu.start();
不能放在声明变量的地方!!!!
你或者放在函数里面或者放在代码块中!!!凭你喜好决定还有!
currentThread是Thread的一个方法!!而不是A的方法,所以前面要加上类名
还有就是if(money=20)应该是if(money==20)这个错误,

2007-10-26 11:30
HCL
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-6-13
收藏
得分:0 

虽然不知道楼主写的程序是基于什么问题写的,偶改了一下,贴出来大家参考一下,有问题的话请高手直接指出来,谢谢!

class A implements Runnable
{
private B a;
public A(){
a = new B();
}
public void run()
{
if(Thread.currentThread().getName().equals("zhangsan"))
{
a.course(20);
}
if(Thread.currentThread().getName().equals("lisi"))
{
a.course(10);
}
if(Thread.currentThread().getName().equals("wangwu"))
{
a.course(5);
}
}
}

class B
{
public int money5=3;
public int money10=0;
public int money20=0;
public void course(int money)
{
if(money==5)
{
money5=money5+1;
System.out.println("你给的钱刚好");
}
if(money==10)
{
while(money5<1)
{
try
{
Thread.sleep(200);
}
catch(InterruptedException e){
}
money5=money5-1;
money10=money10+1;
System.out.println("你给我10元,我找你5元");
}
}
if(money==20)
{
while(money5<3)
{
try
{
Thread.sleep(200);
}
catch(InterruptedException e){
}
money5=money5-3;
money20=money20+1;
System.out.println("你给我20元,我找你15元");
}
}
}
}


public class ThreadUse{
public static void main(String[] argv)
{
Thread zhangsan=new Thread(new A(),"zhangsan");
Thread lisi=new Thread(new A(),"lisi");
Thread wangwu=new Thread(new A(),"wangwu");

zhangsan.start();
lisi.start();
wangwu.start();
}
}

还有一个想说明的是:只有在同步控制方法或同步控制块中调用wait(),notify()跟notifyAll(),
否则可能会有异常(IllegalMonitorStateException)
而sleep()在非同步控制方法里用


2007-10-26 12:47
syuanq
Rank: 2
等 级:新手上路
威 望:3
帖 子:297
专家分:0
注 册:2006-12-11
收藏
得分:0 

回楼上的。。。这样好像只启动了一个线程喔


[url]www.[/url]欢迎大家的光临,一起交流学习
2007-10-26 13:09
syuanq
Rank: 2
等 级:新手上路
威 望:3
帖 子:297
专家分:0
注 册:2006-12-11
收藏
得分:0 

class A implements Runnable
{
private B a;
public A(){
a = new B();
}
public void run()
{
if(Thread.currentThread().getName().equals("zhangsan"))
{
a.course(20);
}
else if(Thread.currentThread().getName().equals("lisi"))
{
a.course(10);
}
else if(Thread.currentThread().getName().equals("wangwu"))
{
a.course(5);
}
}
}

class B
{
public int money5=3;
public int money10=0;
public int money20=0;
public synchronized void course(int money)
{
if(money==5)
{
money5=money5+1;
System.out.println("你给的钱刚好");
}
else if(money==10)
{
while(money5<1)
{
try
{
wait();
}
catch(InterruptedException e){
}
money5=money5-1;
money10=money10+1;
System.out.println("你给我10元,我找你5元");
}
}
else if(money==20)
{
while(money5<3)
{
try
{
wait();
}
catch(InterruptedException e){
}
money5=money5-3;
money20=money20+1;
System.out.println("你给我20元,我找你15元");
}
}
notifyAll();
}
}


class ThreadUse{
public static void main(String[] args)
{
Thread zhangsan=new Thread(new A(),"zhangsan");
Thread lisi=new Thread(new A(),"lisi");
Thread wangwu=new Thread(new A(),"wangwu");

zhangsan.start();
lisi.start();
wangwu.start();
}
}
现在改为这样,不过好像用nitifyAll()方法没有让线程继续执行


[url]www.[/url]欢迎大家的光临,一起交流学习
2007-10-26 13:39
快速回复:有关线程程序的错误
数据加载中...
 
   



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

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