当前位置: 代码迷 >> Java相关 >> 有关线程程序的错误
  详细解决方案

有关线程程序的错误

热度:182   发布时间:2007-10-24 13:41:45.0
有关线程程序的错误

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  

----------------解决方案--------------------------------------------------------
zhangsan.start();
lisi.start();
wangwu.start();
最好是把这几个放在RUN后面。thread(this)里的this,不要。
----------------解决方案--------------------------------------------------------
换了还是不行呢?this表示实现Runnable接口的实例啊
----------------解决方案--------------------------------------------------------
this没有错。。

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

程序总得有个入口吧!


----------------解决方案--------------------------------------------------------
加了main方法后仍然报错哦。。。报错如下:
--------------------Configuration: <Default>--------------------
F:\java\syjc\ThreadTest.java:317: 非法的表达式开始
public void run()
^
F:\java\syjc\ThreadTest.java:332: 需要 ';'

^
2 错误
----------------解决方案--------------------------------------------------------
一直找不出到底哪里出错了
----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

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

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()在非同步控制方法里用



----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

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()方法没有让线程继续执行


----------------解决方案--------------------------------------------------------
  相关解决方案