当前位置:编程学习 > JAVA >>

多线程问题

刚才无意间发现一个问题
public class J {
    public static void main(String[] args) {
Thread a=new Thread(new Method());
a.start();

    Thread b=new Thread(new Method());
b.start();

    }
 }

public class Method implements Runnable{

//String a = "a";
int b = 1;
String a = new String("a");
public  static int cp=100;
public void run(){
while(true){
synchronized(a){{
if(cp>0){
System.out.println(Thread.currentThread().getName()+"   "+(101-cp));
cp--;
}else{break;}}
}
}
}

}
用string a = "a";的时候打印出来正常的,但是用string a = new String("a");会打印两个1,不是很明白为什么 java --------------------编程问答-------------------- 为啥没人回答~~~~
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,