做了一个线程的试验,结果不能跳出for循环,怎么回事?望解答
public class Mythread {
public static void main(String[] args) {
new Extendsthread("wo");
new Extendsthread("my");
Thread m=Thread.currentThread();
System.out.println("this is main thread...."
+ " " + m.getName());
System.out.println("the next express: " + m);
}
}
class Extendsthread extends Thread{
Thread t;
Extendsthread(String name){
t=new Thread(this,name);
t.start();
}
public void run(){
for(int i=0;i<5;i++){
System.out.println(t.getName()+i);
if(t.getName()=="wo")
break;
System.out.println("skip break!!!!");
System.out.println("this is child thread: "
+ i + t +" \n" + t.getName());
}
}
}
== 用于对象比较时比较的是内存地址,所以if是永假的;对象要用equals比较
在这里不能用break;continue,你可以试一试,不要用break,它只是打断,continue是打断后直接跳出
将==换成equals即可。
字符串比较要用equals 吧。。。
字符串是用equals
楼上说得对,比较字符串用equals