共享锁(读写锁)

1.指该锁可被多个线程所持有

不加出现问题

代码如下:

package cn.link.lock;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class MyLock {
    private volatile Map map = new HashMap<>();
    private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();

    public void put(String key, Object value) {
        // rwLock.writeLock().lock();
        /*try {

        }catch (Exception e){

        }finally {
            rwLock.writeLock().unlock();
        }*/
        System.out.println(Thread.currentThread().getName() + "\t正在写入:" + key);
        try {
            TimeUnit.MILLISECONDS.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        map.put(key, value);
        System.out.println(Thread.currentThread().getName() + "\t写入完成:" + key);
    }

    public void get(String key) {
        //rwLock.writeLock().lock();
        /*try {

        }catch (Exception e){

        }finally {
            rwLock.writeLock().unlock();
        }*/
        System.out.println(Thread.currentThread().getName() + "\t正在读取");
        try {
            TimeUnit.MILLISECONDS.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Object value = map.get(key);
        System.out.println(Thread.currentThread().getName() + "\t读取完成:" + value);
    }

    public static void main(String[] args) {
        MyLock myLock = new MyLock();

        for (int i = 1; i <= 5; i++) {
            final int tempInt = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myLock.put(tempInt + "", tempInt + "");
                }
            }, "" + i).start();
        }

        for (int i = 1; i <= 5; i++) {
            final int tempInt = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myLock.get(tempInt + "");
                }
            }, "" + i).start();
        }
    }
}

运行结果如下:

1    正在写入:1
2    正在写入:2
3    正在写入:3
4    正在写入:4
5    正在写入:5
1    正在读取
2    正在读取
3    正在读取
4    正在读取
5    正在读取
3    写入完成:3
2    写入完成:2
1    写入完成:1
4    写入完成:4
5    写入完成:5
1    读取完成:null
2    读取完成:null
4    读取完成:4
3    读取完成:3
5    读取完成:5

写操作:原子+独占 (整个过程是一个完整统一,中间不可被打断)

加了读写锁

package cn.link.lock;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class MyLock {
    private volatile Map map = new HashMap<>();
    private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();

    public void put(String key, Object value) {
        try {
            rwLock.writeLock().lock();
            System.out.println(Thread.currentThread().getName() + "\t正在写入:" + key);
            try {
                TimeUnit.MILLISECONDS.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rwLock.writeLock().unlock();
        }

        map.put(key, value);
        System.out.println(Thread.currentThread().getName() + "\t写入完成:" + key);
    }

    public void get(String key) {
        try {
            rwLock.readLock().lock();
            System.out.println(Thread.currentThread().getName() + "\t正在读取");
            try {
                TimeUnit.MILLISECONDS.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Object value = map.get(key);
            System.out.println(Thread.currentThread().getName() + "\t读取完成:" + value);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rwLock.readLock().unlock();
        }

    }

    public static void main(String[] args) {
        MyLock myLock = new MyLock();

        for (int i = 1; i <= 5; i++) {
            final int tempInt = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myLock.put(tempInt + "", tempInt + "");
                }
            }, "" + i).start();
        }

        for (int i = 1; i <= 5; i++) {
            final int tempInt = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myLock.get(tempInt + "");
                }
            }, "" + i).start();
        }
    }
}

 

运行结果:

1    正在写入:1
1    写入完成:1
2    正在写入:2
2    写入完成:2
4    正在写入:4
4    写入完成:4
3    正在写入:3
3    写入完成:3
5    正在写入:5
5    写入完成:5
1    正在读取
2    正在读取
3    正在读取
4    正在读取
5    正在读取
1    读取完成:1
2    读取完成:2
4    读取完成:4
3    读取完成:3
5    读取完成:5

你可能感兴趣的:(java多线程)