3.Lock对象锁
除了使用synchronized外,还可以使用Lock对象来创建临界区。Resource3.java的演示效果同Resource1.java;Resource4.java的演示效果同Resource2.java。
Resource3.java
packagecom.zj.lock;
importjava.util.concurrent.TimeUnit;
importjava.util.concurrent.locks.Lock;
importjava.util.concurrent.locks.ReentrantLock;
publicclassResource3 {
privateLocklock=newReentrantLock();
publicvoidf() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+":not synchronized in f()");
lock.lock();
try{
for(inti = 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();
}
}
publicvoidg() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+":not synchronized in g()");
lock.lock();
try{
for(inti = 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();
}
}
publicvoidh() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+":not synchronized in h()");
lock.lock();
try{
for(inti = 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();
}
}
publicstaticvoidmain(String[] args) {
finalResource3 rs =newResource3();
newThread() {
publicvoidrun() {
rs.f();
}
}.start();
newThread() {
publicvoidrun() {
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
packagecom.zj.lock;
importjava.util.concurrent.TimeUnit;
importjava.util.concurrent.locks.Lock;
importjava.util.concurrent.locks.ReentrantLock;
publicclassResource4 {
privateLocklock1=newReentrantLock();
privateLocklock2=newReentrantLock();
privateLocklock3=newReentrantLock();
publicvoidf() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+":not synchronized in f()");
lock1.lock();
try{
for(inti = 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();
}
}
publicvoidg() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+":not synchronized in g()");
lock2.lock();
try{
for(inti = 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();
}
}
publicvoidh() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+":not synchronized in h()");
lock3.lock();
try{
for(inti = 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();
}
}
publicstaticvoidmain(String[] args) {
finalResource4 rs =newResource4();
newThread() {
publicvoidrun() {
rs.f();
}
}.start();
newThread() {
publicvoidrun() {
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()