# Java并发编程系列——锁

2020 年 05 月 02 日 阅读数: 38

lock.lock();try {  doSomething();} finally {  lock.unlock();}

public class ShowLock {    public static void main(String[] args) {        for (int i = 0; i < 10; i++) {            new Thread(new LockRunnable()).start();        }    }    private static class LockRunnable implements Runnable {        private static final Lock lock = new ReentrantLock();        @Override        public void run() {            System.out.printf("Thread %d will get lock\n",                    Thread.currentThread().getId());            lock.lock();            System.out.printf("Thread %d got the lock\n",                    Thread.currentThread().getId());            try {                Thread.sleep(200);            } catch (InterruptedException e) {                e.printStackTrace();            } finally {                lock.unlock();                System.out.printf("Thread %d released the lock\n",                        Thread.currentThread().getId());            }        }    }}

Thread 12 will get lockThread 21 will get lockThread 20 will get lockThread 19 will get lockThread 18 will get lockThread 17 will get lockThread 16 will get lockThread 15 will get lockThread 14 will get lockThread 13 will get lockThread 14 got the lockThread 15 got the lockThread 16 got the lockThread 17 got the lockThread 18 got the lockThread 19 got the lockThread 20 got the lockThread 21 got the lockThread 12 got the lockThread 13 got the lockThread 14 released the lockThread 13 released the lockThread 21 released the lockThread 19 released the lockThread 20 released the lockThread 15 released the lockThread 17 released the lockThread 18 released the lockThread 16 released the lockThread 12 released the lock

public class ShowCondition {    public static void main(String[] args) throws InterruptedException {        Show show = new Show();        for (int i = 0; i < 3; i++) {            new Thread(() -> show.checkCondition2()).start();            new Thread(() -> show.checkCondition1()).start();        }        Thread.sleep(2000);        System.out.println();        show.changeCondition1(8);        Thread.sleep(1000);        System.out.println();        show.changeCondition1(11);        Thread.sleep(1000);        System.out.println();        show.changeCondition2(true);        Thread.sleep(1000);    }    private static class Show {        private final Lock lock = new ReentrantLock();        private final Condition lockCondition1 = lock.newCondition();        private final Condition lockCondition2 = lock.newCondition();        private int condition1 = 0;        private boolean condition2 = false;        public void checkCondition1() {            lock.lock();            try {                while (this.condition1 <= 10) {                    try {                        System.out.printf("thread %d start check conditions1 now\n",                                Thread.currentThread().getId());                        lockCondition1.await();                        System.out.printf("condition1 is %d, waiting in thread  %d\n",                                this.condition1, Thread.currentThread().getId());                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                System.out.printf("condition1 is %d now, bigger than 10, thread %d over\n",                        this.condition1, Thread.currentThread().getId());            } finally {                lock.unlock();            }        }        public void checkCondition2() {            lock.lock();            try {                while (!condition2) {                    try {                        System.out.printf("thread %d start check conditions2 now\n",                                Thread.currentThread().getId());                        lockCondition2.await();                        System.out.printf("condition2 is false, waiting in thread %d\n",                                Thread.currentThread().getId());                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                System.out.printf("condition2 is true now, thread %d over\n",                        Thread.currentThread().getId());            } finally {                lock.unlock();            }        }        public void changeCondition1(int condition1) {            lock.lock();            try {                this.condition1 = condition1;                lockCondition1.signalAll();            } finally {                lock.unlock();            }        }        public void changeCondition2(boolean condition2) {            lock.lock();            try {                this.condition2 = condition2;                lockCondition2.signalAll();            } finally {                lock.unlock();            }        }    }}

public class ShowReadWriteLock {    private static ReadWrite readWrite = new ReadWrite();    public static void main(String[] args) {        for (int i = 0; i < 3; i++) {            new Thread(new WriteRunnable()).start();            for (int j = 0; j < 10; j++) {                new Thread(new ReadRunnable()).start();            }        }    }    private static class ReadRunnable implements Runnable {        @Override        public void run() {            int total = readWrite.getTotal();        }    }    private static class WriteRunnable implements Runnable {        @Override        public void run() {            int random = new Random().nextInt(100);            readWrite.setTotal(random);        }    }    private static class ReadWrite {        private final ReadWriteLock lock = new ReentrantReadWriteLock();        private final Lock readLock = lock.readLock();        private final Lock writeLock = lock.writeLock();        private int total;        public int getTotal() {            long start = System.currentTimeMillis();            readLock.lock();            try {                Thread.sleep(10);            } catch (InterruptedException e) {                e.printStackTrace();            } finally {                System.out.printf("the total is %d and get total in %d ms\n",                        total, System.currentTimeMillis() - start);                readLock.unlock();            }            return this.total;        }        public void setTotal(int total) {            long start = System.currentTimeMillis();            writeLock.lock();            try {                Thread.sleep(10);                this.total = total;                System.out.printf("set total %d in %d ms\n",                        total, System.currentTimeMillis() - start);            } catch (InterruptedException e) {                e.printStackTrace();            } finally {                writeLock.unlock();            }        }    }}

set total 0 in 12 msthe total is 0 and get total in 53 msthe total is 0 and get total in 51 msthe total is 0 and get total in 52 msthe total is 0 and get total in 53 msthe total is 0 and get total in 52 msthe total is 0 and get total in 51 msthe total is 0 and get total in 51 msthe total is 0 and get total in 51 msthe total is 0 and get total in 52 msthe total is 0 and get total in 53 msset total 75 in 66 msthe total is 75 and get total in 76 msthe total is 75 and get total in 77 msthe total is 75 and get total in 76 msthe total is 75 and get total in 77 msthe total is 75 and get total in 77 msthe total is 75 and get total in 77 msthe total is 75 and get total in 76 msthe total is 75 and get total in 76 msthe total is 75 and get total in 77 msthe total is 75 and get total in 76 msset total 59 in 89 msthe total is 59 and get total in 99 msthe total is 59 and get total in 98 msthe total is 59 and get total in 98 msthe total is 59 and get total in 98 msthe total is 59 and get total in 99 msthe total is 59 and get total in 99 msthe total is 59 and get total in 99 msthe total is 59 and get total in 99 msthe total is 59 and get total in 99 msthe total is 59 and get total in 102 ms

set total 85 in 10 msthe total is 85 and get total in 54 msthe total is 85 and get total in 63 msthe total is 85 and get total in 76 msthe total is 85 and get total in 87 msthe total is 85 and get total in 98 msthe total is 85 and get total in 109 msthe total is 85 and get total in 120 msthe total is 85 and get total in 130 msthe total is 85 and get total in 141 msthe total is 85 and get total in 154 msset total 70 in 166 msthe total is 70 and get total in 175 msthe total is 70 and get total in 189 msthe total is 70 and get total in 201 msthe total is 70 and get total in 213 msthe total is 70 and get total in 224 msthe total is 70 and get total in 238 msthe total is 70 and get total in 250 msthe total is 70 and get total in 261 msthe total is 70 and get total in 273 msthe total is 70 and get total in 285 msset total 73 in 295 msthe total is 73 and get total in 305 msthe total is 73 and get total in 316 msthe total is 73 and get total in 329 msthe total is 73 and get total in 343 msthe total is 73 and get total in 357 msthe total is 73 and get total in 369 msthe total is 73 and get total in 380 msthe total is 73 and get total in 394 msthe total is 73 and get total in 406 msthe total is 73 and get total in 417 ms

Java并发编程系列——常用并发工具类

Java并发编程系列——Fork-Join

Java并发编程系列——线程的等待与唤醒

Java并发编程系列插曲——对象的内存结构

Java并发编程系列——线程