我期待下面的增加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