本帖最后由 汤密奕 于 2012-7-9 14:51 编辑
class SynTest implements Runnable{
int i = 10;
public synchronized void method1()
{
i = 11;
System.out.println("method1:"+i);
}
public synchronized void method2()
{
try { Thread.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
}
i = 12;
}
public void run()
{
method1();
}
public static void main(String[] args) {
SynTest s = new SynTest();
Thread t = new Thread(s);
t.start();
s.method2();
System.out.println("main:"+s.i);
}
}
1、对此代码不改,结果是?
2、去掉method2()方法的同步 ,结果是?
3、在method2()方法去掉Thread.sleep(25000)的睡眠,结果是?麻烦分别陈述三种运行结果的原因
|
|