lock 读写锁

package function.thread;

import java.util.Random;

import java.util.concurrent.locks.ReadWriteLock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class LockTest {

public static void main(String args[]){

final ShareResourceOper sro = new ShareResourceOper();

        for (int i = 0; i < 3; i++) {

            new Thread() {

                public void run() {

                    while (true) {

                    sro.get();

                    }

                }

            }.start();//三个消费者

            new Thread() {

                public void run() {

                    while (true) {

                    sro.put(new Random().nextInt(10000));

                    }

                }

            }.start();//三个生产者

        }

}

}

class ShareResourceOper {

    private Object shareData = null;// 共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。

    //读写锁

    ReadWriteLock rwl = new ReentrantReadWriteLock();

    // 相当于读操作

    public void get() {

        rwl.readLock().lock();

        String threadName = Thread.currentThread().getName();

        try {

            System.out.println("**************** read thread "+threadName + " be ready to read data!");

            Thread.sleep(1000);

            if(this.shareData !=null && !this.shareData.equals("")){

            System.out.println(" read thread "+threadName+ " have read data :" + shareData);

            }else{

            System.out.println(" read thread "+threadName+ " empty !");

            }

            

        } catch (InterruptedException e) {

            e.printStackTrace();

        } finally {

        System.out.println("**************** read thread "+threadName+ " release the lock !");

        //可能执行完该语句,读线程就抢占了CPU,要想精准看出锁的同步排斥机制,system语句需要放在锁语句的前面

            rwl.readLock().unlock();

            //System.out.println("**************** read thread "+threadName+ " release the lock !");

        }

    }

    // 相当于写操作

    public void put(Object data) {

        rwl.writeLock().lock();

        String threadName = Thread.currentThread().getName();

        try {

            System.out.println("**************** write thread "+threadName+ " be ready to write data!");

            Thread.sleep(1000);

            this.shareData = data;

            System.out.println(" write thread "+threadName+ " have write data: " + data);

        } catch (InterruptedException e) {

            e.printStackTrace();

        } finally {

        System.out.println("**************** write thread "+threadName+ " release the lock !");

            rwl.writeLock().unlock();

            

        }

    }

}

运行结果:

**************** read thread Thread-0 be ready to read data!

**************** read thread Thread-2 be ready to read data!

**************** read thread Thread-4 be ready to read data!

 read thread Thread-0 empty !

**************** read thread Thread-0 release the lock !

 read thread Thread-2 empty !

**************** read thread Thread-2 release the lock !

 read thread Thread-4 empty !

**************** read thread Thread-4 release the lock !

**************** write thread Thread-3 be ready to write data!

 write thread Thread-3 have write data: 692

**************** write thread Thread-3 release the lock !

**************** write thread Thread-3 be ready to write data!

 write thread Thread-3 have write data: 349

**************** write thread Thread-3 release the lock !

**************** write thread Thread-1 be ready to write data!

 write thread Thread-1 have write data: 6981

**************** write thread Thread-1 release the lock !

**************** write thread Thread-1 be ready to write data!

 write thread Thread-1 have write data: 3742

**************** write thread Thread-1 release the lock !

**************** write thread Thread-1 be ready to write data!

 write thread Thread-1 have write data: 2155

**************** write thread Thread-1 release the lock !

**************** write thread Thread-5 be ready to write data!

 write thread Thread-5 have write data: 6080

**************** write thread Thread-5 release the lock !

**************** write thread Thread-5 be ready to write data!

 write thread Thread-5 have write data: 1327

**************** write thread Thread-5 release the lock !

**************** write thread Thread-5 be ready to write data!

 write thread Thread-5 have write data: 9358

**************** write thread Thread-5 release the lock !

**************** read thread Thread-0 be ready to read data!

**************** read thread Thread-2 be ready to read data!

**************** read thread Thread-4 be ready to read data!

 read thread Thread-0 have read data :9358

**************** read thread Thread-0 release the lock !

 read thread Thread-4 have read data :9358

**************** read thread Thread-4 release the lock !

 read thread Thread-2 have read data :9358

**************** read thread Thread-2 release the lock !

**************** write thread Thread-3 be ready to write data!

 write thread Thread-3 have write data: 3529

**************** write thread Thread-3 release the lock !

**************** write thread Thread-3 be ready to write data!

 write thread Thread-3 have write data: 9146

**************** write thread Thread-3 release the lock !

**************** write thread Thread-1 be ready to write data!

 write thread Thread-1 have write data: 1339

**************** write thread Thread-1 release the lock !

**************** write thread Thread-1 be ready to write data!

 write thread Thread-1 have write data: 4327

**************** write thread Thread-1 release the lock !

**************** write thread Thread-5 be ready to write data!

 write thread Thread-5 have write data: 2020

**************** write thread Thread-5 release the lock !

**************** read thread Thread-0 be ready to read data!

**************** read thread Thread-4 be ready to read data!

**************** read thread Thread-2 be ready to read data!

 read thread Thread-0 have read data :2020

**************** read thread Thread-0 release the lock !

 read thread Thread-4 have read data :2020

**************** read thread Thread-4 release the lock !

 read thread Thread-2 have read data :2020

**************** read thread Thread-2 release the lock !

**************** write thread Thread-3 be ready to write data!

 write thread Thread-3 have write data: 633

**************** write thread Thread-3 release the lock !

**************** write thread Thread-3 be ready to write data!

 write thread Thread-3 have write data: 5760

**************** write thread Thread-3 release the lock !

**************** write thread Thread-3 be ready to write data!

 write thread Thread-3 have write data: 2856

**************** write thread Thread-3 release the lock !

**************** write thread Thread-3 be ready to write data!

 write thread Thread-3 have write data: 1045

**************** write thread Thread-3 release the lock !

**************** write thread Thread-1 be ready to write data!

猜你喜欢

转载自zengshaotao.iteye.com/blog/2373328