zoukankan      html  css  js  c++  java
  • Java多线程-Lock的使用(1)

    ReentrantLock类

    在JDK1.5中,新增了 ReentrantLock,相比 synchronized 在扩展功能上更加强大,比如具有嗅探锁定、多路分支通知等功能。

    示例:

    public class Test {
        public static void main(String[] args) {
            MyService service = new MyService();
            new Thread(() -> service.methodA()).start();
            new Thread(() -> service.methodB()).start();
        }
    
        static class MyService {
            private Lock lock = new ReentrantLock();
    
            public void methodA() {
                lock.lock();
                try {
                    System.out.println("methodA begin ThreadName = " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
                    Thread.sleep(5000);
                    System.out.println("methodA end ThreadName = " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
    
            public void methodB() {
                lock.lock();
                try {
                    System.out.println("methodB begin ThreadName = " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
                    Thread.sleep(5000);
                    System.out.println("methodB end ThreadName = " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    运行结果如图:

    使用Condition 实现等待/通知

    public class ConditionTest {
        public static void main(String[] args) throws InterruptedException {
            Service service = new Service();
            new Thread(() -> service.await()).start();
            Thread.sleep(3000);
            service.signal();
        }
    
        static class Service {
            private Lock lock = new ReentrantLock();
            private Condition condition = lock.newCondition();
    
            public void await() {
                lock.lock();
                try {
                    System.out.println("begin await 时间为" + System.currentTimeMillis());
                    condition.await();
                    System.out.println("end await 时间为" + System.currentTimeMillis());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
    
            public void signal() {
                lock.lock();
                try {
                    System.out.println("begin signal 时间为" + System.currentTimeMillis());
                    condition.signal();
                    System.out.println("end signal 时间为" + System.currentTimeMillis());
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    运行结果如图:

    使用Condition实现多对多生产者/消费者模式

    public class Test {
        public static void main(String[] args) {
            Service service = new Service();
            for (int i = 0; i < 10; i++) {
                new Thread(() -> {
                    for (int j = 0; j < Integer.MAX_VALUE; j++) {
                        service.set();
                    }
                }).start();
                new Thread(() -> {
                    for (int j = 0; j < Integer.MAX_VALUE; j++) {
                        service.get();
                    }
                }).start();
            }
        }
    
        static class Service {
            private Lock lock = new ReentrantLock();
            private Condition condition = lock.newCondition();
            private boolean hasValue = false;
    
            public void set() {
                lock.lock();
                try {
                    while (hasValue == true) {
                        System.out.println("有可能++连续");
                        condition.await();
                    }
                    System.out.println("打印+");
                    hasValue = true;
                    condition.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
    
            public void get() {
                lock.lock();
                try {
                    while (hasValue == false) {
                        System.out.println("有可能**连续");
                        condition.await();
                    }
                    System.out.println("打印*");
                    hasValue = false;
                    condition.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    运行结果如图:

  • 相关阅读:
    关于javaScript substring()方法的一点点应用
    解决Vue报错:Invalid prop: type check failed for prop "id". Expected Number with value 14, got String with value "14".
    better-scroll滚动无效的查错
    Vue程序化导航---又称编程式导航
    Vue路由初始化Can't read prooerty '$createElement' of undefined
    在前台利用jquery对dom元素进行排序
    js/jquery如何获取获取父窗口的父窗口的元素
    jquery使鼠标滚轮暂时失效
    introJs写法
    用Intro.js创建站点分步指南
  • 原文地址:https://www.cnblogs.com/lkc9/p/12562590.html
Copyright © 2011-2022 走看看