Java中锁的机制

一、采用synchronized关键字。

实现用时就是放该关键字到方法前面,表示该方法是同步的,同一时间只能有一个线程使用它。但是有区别:


1、静态方法前加synchronized关键字表示所有此类的不同实例调用这个方法的时候都是串行的。
2、非静态方法前标识某一个实例被不同的线程调用时是串行的,但是不同实例之间是并行的。


例如:

  1. /**
  2. * 测试synchronized的区别
  3. *
  4. * @author <a href="mailto:HL_Qu@hotmail.com">Along</a>
  5. *
  6. * @version $Revision$
  7. *
  8. * @since 2009-3-24
  9. */
  10. public class TestSynchronized implements Runnable {

  11.     /**
  12.      * 所有类实例等待一个锁
  13.      */
  14.     private synchronized static void test() throws InterruptedException {
  15.         System.out.println("Obj= in.");
  16.         Thread.sleep(4000);
  17.         System.out.println("Obj= out.");
  18.     }
  19.    
  20.     /**
  21.      * 同一个实例的不同线程等待一个锁
  22.      */
  23.     private synchronized void test2() throws InterruptedException {
  24.         System.out.println("Obj=" + this + "in.");
  25.         Thread.sleep(4000);
  26.         System.out.println("Obj=" + this + "out.");
  27.     }

  28.     @Override
  29.     public void run() {
  30.         try {
  31.             test();
  32.             this.test2();
  33.         } catch (InterruptedException e) {
  34.             e.printStackTrace();
  35.         }
  36.     }

  37.     public static void main(String[] args) {
  38.         TestSynchronized a = new TestSynchronized();
  39.         TestSynchronized b = new TestSynchronized();

  40.         new Thread(a).start();
  41.         new Thread(b).start();
  42.     }
  43. }

输出:

  1. Obj= in.
  2. Obj= out.
  3. Obj= in.
  4. Obj=TestSynchronized@61de33in.
  5. Obj= out.
  6. Obj=TestSynchronized@ca0b6in.
  7. Obj=TestSynchronized@61de33out.
  8. Obj=TestSynchronized@ca0b6out.

synchronized还有一种使用方式就是同步代码块。例如:

  1. /**
  2. * 模拟静态方法锁机制
  3. *
  4. * @author <a href="mailto:HL_Qu@hotmail.com">Along</a>
  5. *
  6. * @version $Revision$
  7. *
  8. * @since 2009-3-24
  9. */
  10. public class TestLock2 implements Runnable {

  11.     private static String lock = new String("Locker");

  12.     /**
  13.      * 所有类实例等待一个锁
  14.      */
  15.     private void test() {
  16.         System.out.println("Obj=" + this + "in.");
  17.         synchronized (lock) {
  18.             System.out.println("Obj=" + this + "Locked.");
  19.             try {
  20.                 Thread.sleep(4000);
  21.             } catch (InterruptedException e) {
  22.                 e.printStackTrace();
  23.             }
  24.             System.out.println("Obj=" + this + "Unlocked.");
  25.         }

  26.         System.out.println("Obj=" + this + "out.");
  27.     }
  28.    
  29.     /**
  30.      * 同一个实例的不同线程等待一个锁
  31.      */
  32.     private void test2() {
  33.         System.out.println("Obj=" + this + "in.");
  34.         synchronized (this) {
  35.             System.out.println("Obj=" + this + "Locked.");
  36.             try {
  37.                 Thread.sleep(4000);
  38.             } catch (InterruptedException e) {
  39.                 e.printStackTrace();
  40.             }
  41.             System.out.println("Obj=" + this + "Unlocked.");
  42.         }

  43.         System.out.println("Obj=" + this + "out.");
  44.     }
  45.    
  46.     @Override
  47.     public void run() {
  48.         this.test2();
  49.         this.test();
  50.     }
  51.    
  52.     public static void main(String[] args) {
  53.         TestLock2 a = new TestLock2();
  54.         TestLock2 b = new TestLock2();

  55.         new Thread(a).start();
  56.         new Thread(b).start();
  57.     }

  58. }

输出:

  1. Obj=TestLock2@61de33in.
  2. Obj=TestLock2@61de33Locked.
  3. Obj=TestLock2@14318bbin.
  4. Obj=TestLock2@14318bbLocked.
  5. Obj=TestLock2@14318bbUnlocked.
  6. Obj=TestLock2@14318bbout.
  7. Obj=TestLock2@14318bbin.
  8. Obj=TestLock2@14318bbLocked.
  9. Obj=TestLock2@61de33Unlocked.
  10. Obj=TestLock2@61de33out.
  11. Obj=TestLock2@61de33in.
  12. Obj=TestLock2@14318bbUnlocked.
  13. Obj=TestLock2@14318bbout.
  14. Obj=TestLock2@61de33Locked.
  15. Obj=TestLock2@61de33Unlocked.
  16. Obj=TestLock2@61de33out.

二、采用Java提供的并发类实现。

例如使用ReentrantLock(一个可重入的互斥锁 Lock,它具有与使用 synchronized 方法和语句所访问的隐式监视器锁相同的一些基本行为和语义,但功能更强大。)

代码:

  1. import java.util.concurrent.locks.ReentrantLock;

  2. /**
  3. * 用Java提供的工具类模拟静态方法锁机制
  4. *
  5. *
  6. * @author <a href="mailto:HL_Qu@hotmail.com">Along</a>
  7. *
  8. * @version $Revision$
  9. *
  10. * @since 2009-3-24
  11. */
  12. public class TestLock implements Runnable {

  13.     private static ReentrantLock lock = new ReentrantLock();

  14.     @Override
  15.     public void run() {
  16.         System.out.println("Obj=" + this + "in.");
  17.         if (lock.isLocked()) {
  18.             System.out.println("Method has been Locked.");
  19.         }
  20.         
  21.         lock.lock();
  22.         System.out.println("Obj=" + this + "Locked.");
  23.         try {
  24.             Thread.sleep(4000);
  25.         } catch (InterruptedException e) {
  26.             e.printStackTrace();
  27.         }
  28.         System.out.println("Obj=" + this + "Unlocked.");
  29.         lock.unlock();
  30.         
  31.         System.out.println("Obj=" + this + "out.");
  32.     }
  33.    
  34.     public static void main(String[] args) {
  35.         TestLock a = new TestLock();
  36.         TestLock b = new TestLock();

  37.         new Thread(a).start();
  38.         new Thread(b).start();
  39.     }

  40. }

输出:

  1. Obj=TestLock@61de33in.
  2. Obj=TestLock@61de33Locked.
  3. Obj=TestLock@14318bbin.
  4. Method has been Locked.
  5. Obj=TestLock@61de33Unlocked.
  6. Obj=TestLock@14318bbLocked.
  7. Obj=TestLock@61de33out.
  8. Obj=TestLock@14318bbUnlocked.
  9. Obj=TestLock@14318bbout.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值