多线程问题
刚才无意间发现一个问题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相关