
急急急!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来实现线程! 展开
{
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来实现线程! 展开
展开全部
兄弟,你的构造方法好像写错了,你提出的问题找不到符号是因为你没有定义过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
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
展开全部
无参构造方法呢?
还有你这里默认的getName()可以得到thread3吗
Returns:
the name of the class or interface represented by this object
可以自己写一个覆盖原有的getName()
还有你这里默认的getName()可以得到thread3吗
Returns:
the name of the class or interface represented by this object
可以自己写一个覆盖原有的getName()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的getName方法在哪定义的。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-10-25
展开全部
你把if(getName().equals("thread3"))改成if(Thread.currentThread()=="thread3")就OK了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询