急急急!Java高手请帮帮忙!

classTestimplementsRunnable{Stringstr;publicvoidTest(Stringstr){this.str=str;}publicv... class Test implements Runnable
{
String str;
public void Test(String str)
{
this.str=str;
}

public void run()
{
try
{

for (int i=0;i<10;i++)
{
Thread.sleep(10);
}

if(getName().equals("thread3"))
{
System.out.println("thread3" + "is runing");
}
else
{
System.out.println("thread4" + "is runing");
}

}
catch (InterruptedException e)
{
}
}
}
public class test
{

public static void main(String args[])
{

Test t3= new Test();
Test t4= new Test();
Thread t = new Thread(t3);
Thread t1 = new Thread(t4);

t.start();
t1.start();
System.out.println("******the programe is running******");

}
}

上面的程序中,if(getName().equals("thread3"))老是说找不到符号,是怎么回事,麻烦告诉下,要改好的程序,谢谢!主要是用runnable来实现线程!
展开
 我来答
Christ_earl
2009-10-26 · TA获得超过1245个赞
知道小有建树答主
回答量:239
采纳率:0%
帮助的人:0
展开全部
兄弟,你的构造方法好像写错了,你提出的问题找不到符号是因为你没有定义过getName()这个方法。帮你改了下:
class Test implements Runnable {
String str;

public Test(String str) {
this.str = str;
}

public String getName() {
return str;
}

public void run() {
try {

for (int i = 0; i < 10; i++) {
Thread.sleep(10);
}

if (this.getName().equals("thread3")) {
System.out.println("thread3" + "is runing");
} else {
System.out.println("thread4" + "is runing");
}

} catch (InterruptedException e) {
}
}
}

public class Test3 {

public static void main(String[] args) throws Exception {

Test t3 = new Test("thread3");
Test t4 = new Test("thread4");
Thread t = new Thread(t3);
Thread t1 = new Thread(t4);

t.start();
t1.start();
System.out.println("******the programe is running******");

}
}
结果为:
****the programe is running******
thread3is runing
thread4is runing
峰20221215
2009-10-25 · TA获得超过273个赞
知道小有建树答主
回答量:219
采纳率:0%
帮助的人:168万
展开全部
无参构造方法呢?
还有你这里默认的getName()可以得到thread3吗
Returns:
the name of the class or interface represented by this object
可以自己写一个覆盖原有的getName()
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
freish
2009-10-25 · TA获得超过2878个赞
知道大有可为答主
回答量:3153
采纳率:0%
帮助的人:2858万
展开全部
你的getName方法在哪定义的。。。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2009-10-25
展开全部
你把if(getName().equals("thread3"))改成if(Thread.currentThread()=="thread3")就OK了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式