利用AtomicInteger实现一把锁

利用AtomicInteger的cas实现一把自定义的锁,要注意解锁的线程一定是那个加锁的线程,否则会出现别的线程把锁释放掉

先定义一个lock异常类
public class LockException extends Exception{
  public LockException(String message) {
    super(message);
  }
}
自定义锁
public class CompareAndSetLock {
  private final AtomicInteger value = new AtomicInteger(0);
  private Thread currentThread;
  public void tryLock() throws LockException{
    final boolean succ = value.compareAndSet(0, 1);
    if (!succ) {
      throw new LockException("get the lock failed.");
    }else {
      currentThread = Thread.currentThread();
    }
  }
  // unlock方法 可以自己想一下
}
public class CompareAndSetLockTest {
  private static final CompareAndSetLock COMPARE_AND_SET_LOCK = new CompareAndSetLock();

  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    List<Thread> workers = new ArrayList<>();
    Arrays.asList("M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8").stream()
            .map(CompareAndSetLockTest::createThread)
            .forEach(
                    t -> {
                      t.start();
                      workers.add(t);
                    });
    workers.forEach(
            t -> {
              try {
                t.join();
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
            });
    System.out.println("消耗时间为");
    System.out.println(System.currentTimeMillis() - start);
  }

  private static Thread createThread(String name) {
    return new Thread(
        () -> {
          try {
            COMPARE_AND_SET_LOCK.tryLock();
            System.out.println(Thread.currentThread().getName() + " do something....");
            Thread.sleep(10000);
          } catch (LockException | InterruptedException e) {
            e.printStackTrace();

          } finally {
            COMPARE_AND_SET_LOCK.unLock();
          }
        },
        name);
  }


  private static Thread createThread1(String name) {
    return new Thread(
            () -> {
              synchronized (CompareAndSetLockTest.class) {
                System.out.println(Thread.currentThread().getName() + " do something....");
                try {
                  Thread.sleep(10000);
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
              }
            },
            name);
  }

  public static void doSomething1() throws LockException, InterruptedException {
    synchronized (CompareAndSetLockTest.class) {
      System.out.println(Thread.currentThread().getName() + "do something....");
      Thread.sleep(10000);
    }
  }
}
我们分别调用createThread和createThread1方法看看区别

createThread结果:

M1 do something....
com.concurrency.thread.atomic.LockException: get the lock failed.
	at com.concurrency.thread.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:17)
	at com.concurrency.thread.atomic.integer.CompareAndSetLockTest.lambda$createThread$2(CompareAndSetLockTest.java:47)
	at java.lang.Thread.run(Thread.java:748)
com.concurrency.thread.atomic.LockException: get the lock failed.
	at com.concurrency.thread.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:17)
	at com.concurrency.thread.atomic.integer.CompareAndSetLockTest.lambda$createThread$2(CompareAndSetLockTest.java:47)
	at java.lang.Thread.run(Thread.java:748)
com.concurrency.thread.atomic.LockException: get the lock failed.
	at com.concurrency.thread.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:17)
	at com.concurrency.thread.atomic.integer.CompareAndSetLockTest.lambda$createThread$2(CompareAndSetLockTest.java:47)
	at java.lang.Thread.run(Thread.java:748)
com.concurrency.thread.atomic.LockException: get the lock failed.
	at com.concurrency.thread.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:17)
	at com.concurrency.thread.atomic.integer.CompareAndSetLockTest.lambda$createThread$2(CompareAndSetLockTest.java:47)
	at java.lang.Thread.run(Thread.java:748)
com.concurrency.thread.atomic.LockException: get the lock failed.
	at com.concurrency.thread.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:17)
	at com.concurrency.thread.atomic.integer.CompareAndSetLockTest.lambda$createThread$2(CompareAndSetLockTest.java:47)
	at java.lang.Thread.run(Thread.java:748)
com.concurrency.thread.atomic.LockException: get the lock failed.
	at com.concurrency.thread.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:17)
	at com.concurrency.thread.atomic.integer.CompareAndSetLockTest.lambda$createThread$2(CompareAndSetLockTest.java:47)
	at java.lang.Thread.run(Thread.java:748)
com.concurrency.thread.atomic.LockException: get the lock failed.
	at com.concurrency.thread.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:17)
	at com.concurrency.thread.atomic.integer.CompareAndSetLockTest.lambda$createThread$2(CompareAndSetLockTest.java:47)
	at java.lang.Thread.run(Thread.java:748)
消耗时间为
10061

Process finished with exit code 0

createThread1结果:

M1 do something....
M8 do something....
M7 do something....
M6 do something....
M5 do something....
M4 do something....
M3 do something....
M2 do something....
消耗时间为
80078

Process finished with exit code 0
自定义的锁如果抢不到就直接释放,synchronized抢不到锁的线程进入阻塞状态,直到抢到锁的线程释放锁后继续争抢锁.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐观的Terry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值