我期待下面的增加c值爲2.但即使在第二個線程啓動後,我總是得到1輸出。值不遞增,線程
package test.main;
public class TestThread implements Runnable {
private int c=0;
@Override
public void run() {
synchronized(this){
c=c+1;
//wait(1000);
go();
}
}
private void go() {
System.out.println("Thread name :"+Thread.currentThread()+" in go() : "+c);
}
public static void main(String[] args) throws InterruptedException {
System.out.println("main()");
Thread t1 = new Thread(new TestThread(),"thread1");
Thread t2 = new Thread(new TestThread(),"thread2");
t1.start();
t2.start();
}
}
謝謝大家,大家都回答正確,但接受這個,因爲這是第一個答案。我沒有足夠的積分爲其他人+1。 – user3448119