ReadWriteLock

    讀寫鎖分爲讀鎖和寫鎖,讀鎖不是互斥的,多個線程能夠同時持有讀鎖,可是寫鎖是互斥的,最有有一個在持有。java

1、讀鎖和讀鎖之間不互斥:

public static void main(String[] args) throws Exception {
        final ReadWriteLock lock = new ReentrantReadWriteLock();
        final Lock readLock = lock.readLock();
        final Lock writeLock = lock.writeLock();
        Thread readThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 5; i++) {
                        readLock.lock();
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println(Thread.currentThread().getName() + "在讀取。。。");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    readLock.unlock();
                }
            }
        });
        Thread readThread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 5; i++) {
                        readLock.lock();
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println(Thread.currentThread().getName() + "在讀取。。。");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    readLock.unlock();
                }
            }
        });
        readThread.start();
        readThread2.start();
    }

2、讀鎖和寫鎖互斥:ide

public static void main(String[] args) throws Exception {
        final ReadWriteLock lock = new ReentrantReadWriteLock();
        final Lock readLock = lock.readLock();
        final Lock writeLock = lock.writeLock();
        Thread write = new Thread(new Runnable() {
            @Override
            public void run() {
                writeLock.lock();
                try {
                    for (int i = 0; i < 5; i++) {
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println(Thread.currentThread().getName() + "在寫入。。。");
                    }
                } catch (InterruptedException e) {

                } finally {
                    writeLock.unlock();
                }
            }
        });
        Thread readThread = new Thread(new Runnable() {
            @Override
            public void run() {
                readLock.lock();
                try {
                    for (int i = 0; i < 5; i++) {
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println(Thread.currentThread().getName() + "在讀取。。。");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    readLock.unlock();
                }
            }
        });
        write.start();
        readThread.start();
    }

3、寫鎖和寫鎖互斥spa

public static void main(String[] args) throws Exception {
        final ReadWriteLock lock = new ReentrantReadWriteLock();
        final Lock readLock = lock.readLock();
        final Lock writeLock = lock.writeLock();
        Thread write = new Thread(new Runnable() {
            @Override
            public void run() {
                writeLock.lock();
                try {
                    for (int i = 0; i < 5; i++) {
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println(Thread.currentThread().getName() + "在寫入。。。");
                    }
                } catch (InterruptedException e) {

                } finally {
                    writeLock.unlock();
                }
            }
        });
        Thread write2 = new Thread(new Runnable() {
            @Override
            public void run() {
                writeLock.lock();
                try {
                    for (int i = 0; i < 5; i++) {
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println(Thread.currentThread().getName() + "在寫入。。。");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    writeLock.unlock();
                }
            }
        });
        write.start();
        write2.start();
    }

相關文章
相關標籤/搜索