对synchronized(this)的一些理解(5) Thread.sleep(500); } catch (InterruptedException ie) { } } } }
结果:
t1 : 4t1 : 3t1 : 2t1 : 1t1 : 0t2 : 4t2 : 3t2 : 2t2 : 1t2 : 0
四、第三个例子同样适用其它同步代码块。也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。
//修改Thread2.m4t2()方法如下:
public synchronized void m4t2() { int i = 5; while( i-- > 0) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(500); } catch (InterruptedException ie) {