public class Resource1 { public void f() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in f()"); synchronized (this) { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in f()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public void g() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in g()"); synchronized (this) { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in g()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public void h() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in h()"); synchronized (this) { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in h()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public static void main(String[] args) { final Resource1 rs = new Resource1();
new Thread() { public void run() { rs.f(); } }.start();
new Thread() { public void run() { rs.g(); } }.start();
rs.h(); } } 结果: Thread-0:not synchronized in f() Thread-0:synchronized in f() main:not synchronized in h() Thread-1:not synchronized in g() Thread-0:synchronized in f() Thread-0:synchronized in f() Thread-0:synchronized in f() Thread-0:synchronized in f() Thread-1:synchronized in g() Thread-1:synchronized in g() Thread-1:synchronized in g() Thread-1:synchronized in g() Thread-1:synchronized in g() main:synchronized in h() main:synchronized in h() main:synchronized in h() main:synchronized in h() main:synchronized in h() 2.2 同步到多个对象锁 Resource1.java演示了三个线程(包括main线程)试图进入某个类的三个不同的方法的同步块中,这些同步块处在不同的方法中,并且是同步到三个不同的对象(synchronized (this),synchronized (syncObject1),synchronized (syncObject2)),所以对它们的方法中的临界资源访问是独立的。 Resource2.java package com.zj.lock; import java.util.concurrent.TimeUnit;
public class Resource2 { private Object syncObject1 = new Object(); private Object syncObject2 = new Object();
public void f() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in f()"); synchronized (this) { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in f()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public void g() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in g()"); synchronized (syncObject1) { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in g()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public void h() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in h()"); synchronized (syncObject2) { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in h()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public static void main(String[] args) { final Resource2 rs = new Resource2();
new Thread() { public void run() { rs.f(); } }.start();
new Thread() { public void run() { rs.g(); } }.start();
rs.h(); } } 结果: Thread-0:not synchronized in f() Thread-0:synchronized in f() main:not synchronized in h() main:synchronized in h() Thread-1:not synchronized in g() Thread-1:synchronized in g() Thread-0:synchronized in f() main:synchronized in h() Thread-1:synchronized in g() Thread-0:synchronized in f() main:synchronized in h() Thread-1:synchronized in g() Thread-0:synchronized in f() main:synchronized in h() Thread-1:synchronized in g() Thread-0:synchronized in f() main:synchronized in h() Thread-1:synchronized in g() 3.Lock对象锁 除了使用synchronized外,还可以使用Lock对象来创建临界区。Resource3.java的演示效果同Resource1.java;Resource4.java的演示效果同Resource2.java。 Resource3.java package com.zj.lock; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;
public class Resource3 { private Lock lock = new ReentrantLock();
public void f() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in f()"); lock.lock(); try { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in f()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock.unlock(); } }
public void g() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in g()"); lock.lock(); try { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in g()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock.unlock(); } }
public void h() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in h()"); lock.lock(); try { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in h()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock.unlock(); } }
public static void main(String[] args) { final Resource3 rs = new Resource3();
new Thread() { public void run() { rs.f(); } }.start();
new Thread() { public void run() { rs.g(); } }.start();
rs.h(); } } 结果: Thread-0:not synchronized in f() Thread-0:synchronized in f() main:not synchronized in h() Thread-1:not synchronized in g() Thread-0:synchronized in f() Thread-0:synchronized in f() Thread-0:synchronized in f() Thread-0:synchronized in f() main:synchronized in h() main:synchronized in h() main:synchronized in h() main:synchronized in h() main:synchronized in h() Thread-1:synchronized in g() Thread-1:synchronized in g() Thread-1:synchronized in g() Thread-1:synchronized in g() Thread-1:synchronized in g() Resource4.java package com.zj.lock; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;
public class Resource4 { private Lock lock1 = new ReentrantLock(); private Lock lock2 = new ReentrantLock(); private Lock lock3 = new ReentrantLock();
public void f() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in f()"); lock1.lock(); try { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in f()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock1.unlock(); } }
public void g() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in g()"); lock2.lock(); try { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in g()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock2.unlock(); } }
public void h() { // other operations should not be locked... System.out.println(Thread.currentThread().getName() + ":not synchronized in h()"); lock3.lock(); try { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + ":synchronized in h()"); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock3.unlock(); } }
public static void main(String[] args) { final Resource4 rs = new Resource4();
new Thread() { public void run() { rs.f(); } }.start();
new Thread() { public void run() { rs.g(); } }.start();
rs.h(); } } 结果: Thread-0:not synchronized in f() Thread-0:synchronized in f() main:not synchronized in h() main:synchronized in h() Thread-1:not synchronized in g() Thread-1:synchronized in g() Thread-0:synchronized in f() main:synchronized in h() Thread-1:synchronized in g() Thread-0:synchronized in f() main:synchronized in h() Thread-1:synchronized in g() Thread-0:synchronized in f() main:synchronized in h() Thread-1:synchronized in g() Thread-0:synchronized in f() main:synchronized in h() Thread-1:synchronized in g() 本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/70300