赞
踩
//参数默认false,不公平锁
ReentrantLock lock = new ReentrantLock();
//公平锁
ReentrantLock lock = new ReentrantLock(true);
// 加锁
lock.lock();
try {
//临界区
} finally {
// 解锁
lock.unlock();
}
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Tset1 { // 定义数据 private static int sum = 0; // 定义自己的锁 private static Lock lock = new ReentrantLock(); public static void main(String[] args) throws InterruptedException { // 循环3吃,开启三个线程 for (int i = 0; i < 3; i++) { Thread thread = new Thread(() -> { // 加锁 lock.lock(); try { // 不加锁这里执行完小于30000 for (int j = 0; j < 10000; j++) { sum++; } } finally { // 解锁 lock.unlock(); } }); thread.start(); } // 等待线程执行完 Thread.sleep(2000); // 打印最后的结果:30000 System.out.println(sum); } }
import java.util.concurrent.locks.ReentrantLock; import lombok.extern.slf4j.Slf4j; @Slf4j public class Tset2 { public static ReentrantLock lock = new ReentrantLock(); public static void main(String[] args) { // 第一个线程中调用第二个线程、第二个线程中调用第三个线程 method1(); } public static void method1() { lock.lock(); try { log.debug("执行 method1"); method2(); } finally { lock.unlock(); } } public static void method2() { lock.lock(); try { log.debug("执行 method2"); method3(); } finally { lock.unlock(); } } public static void method3() { lock.lock(); try { log.debug("执行 method3"); } finally { lock.unlock(); } } }
import java.util.concurrent.locks.ReentrantLock; import lombok.extern.slf4j.Slf4j; @Slf4j public class Tset3 { public static void main(String[] args) { ReentrantLock lock = new ReentrantLock(); Thread t1 = new Thread(() -> { log.debug("t1启动..."); // 注意: 即使是设置的公平锁,此方法也会立即返回获取锁成功或失败,公平策略不生效 if (!lock.tryLock()) { log.debug("t1获取锁失败,立即返回false"); return; } try { log.debug("t1获得了锁"); } finally { lock.unlock(); } }, "t1"); lock.lock(); try { log.debug("main线程获得了锁"); t1.start(); // 先让线程t1执行 try { // 模拟业务 while (true) { } } catch (Exception e) { e.printStackTrace(); } } finally { lock.unlock(); } } }
import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; import lombok.extern.slf4j.Slf4j; @Slf4j public class Tset4 { public static void main(String[] args) { ReentrantLock lock = new ReentrantLock(); Thread t1 = new Thread(() -> { log.debug("t1启动..."); // 超时 try { if (!lock.tryLock(1, TimeUnit.SECONDS)) { log.debug("等待 1s 后获取锁失败,返回"); return; } } catch (InterruptedException e) { e.printStackTrace(); return; } try { log.debug("t1获得了锁"); } finally { lock.unlock(); } }, "t1"); lock.lock(); try { log.debug("main线程获得了锁"); t1.start(); // 先让线程t1执行 try { // 模拟业务 while (true) { } } catch (Exception e) { e.printStackTrace(); } } finally { lock.unlock(); } } }
import java.util.concurrent.locks.ReentrantLock; import lombok.extern.slf4j.Slf4j; @Slf4j public class Tset5 { public static void main(String[] args) throws InterruptedException { ReentrantLock lock = new ReentrantLock(true); // 公平锁 for (int i = 0; i < 500; i++) { new Thread(() -> { lock.lock(); try { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } log.debug(Thread.currentThread().getName() + " running..."); } finally { lock.unlock(); } }, "t" + i).start(); } // 1s 之后去争抢锁 Thread.sleep(1000); for (int i = 0; i < 500; i++) { new Thread(() -> { lock.lock(); try { log.debug(Thread.currentThread().getName() + " running..."); } finally { lock.unlock(); } }, "强行插入" + i).start(); } } }
公平锁运行结果:按照进入的顺序执行,正常顺序日志,无突出的重点,不截图了。
非公平锁运行(定义ReentrantLock的时候不传值)结果:出现插队的线程!
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; import lombok.extern.slf4j.Slf4j; @Slf4j public class Tset6 { private static ReentrantLock lock = new ReentrantLock(); private static Condition cigCon = lock.newCondition(); private static boolean hashcig = false; public static void main(String[] args) { // 定义线程需要被唤醒的线程 new Thread(() -> { lock.lock(); log.debug("需要被唤醒的线程加锁"); try { while (!hashcig) { try { log.debug("需要被唤醒的线程等待!"); cigCon.await(); } catch (Exception e) { e.printStackTrace(); } } log.debug("需要被唤醒的线程跳出循环!"); } finally { lock.unlock(); } }).start(); // 准备要唤醒的线程 new Thread(() -> { lock.lock(); try { // 设置条件变了 hashcig = true; // 唤醒等待线程 cigCon.signal(); } finally { lock.unlock(); } }, "t1").start(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。