ITeye博客

Lock实现线程通信

youyang_java · 2014-04-23 14:27 · 多线程 · 编程语言 · Markdown

Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象。两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象。

 

读写锁:分为读锁和写锁,多个读锁不互斥,读锁与写锁互斥,写锁与写锁互斥,这是由jvm自己控制的,你只要上好相应的锁即可。如果你的代码只读数据,可以很多人同时读,但不能同时写,那就上读锁;如果你的代码修改数据,只能有一个人在写,且不能同时读取,那就上写锁。总之,读的时候上读锁,写的时候上写锁! 

public static void main(String[] args) {
        new ReentrantLockTest().init();

    }
   
    public void init(){
       
        final Printer printer = new Printer();
       
        new Thread(){

            @Override
            public void run() {
               
                while(true){
                   
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    printer.print("youyang");
                }
               
            }
           
           
        }.start();
       
        new Thread(){

            @Override
            public void run() {
               
                while(true){
                   
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    printer.print("yusiyi");
                }
            }
           
           
        }.start();
    }
   
    class Printer {
       
        private Lock lock = new ReentrantLock();
        public void print(String string){
            lock.lock();
            try{
                for(int i=0;i<string.length();i++){
                    System.out.print(string.charAt(i));
                }
                System.out.println();
            }finally{
                lock.unlock();
            }
        }
    }

 

public class ReadWriteLockTest {
    public static void main(String[] args) {
        final Queue3 q3 = new Queue3();
        for(int i=0;i<3;i++)
        {
            new Thread(){
                public void run(){
                    while(true){
                        q3.get();                       
                    }
                }
               
            }.start();

            new Thread(){
                public void run(){
                    while(true){
                        q3.put(new Random().nextInt(10000));
                    }
                }           
               
            }.start();
        }
       
    }
}

class Queue3{
    private Object data = null;//共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。
    ReadWriteLock rwl = new ReentrantReadWriteLock();
    public void get(){
        rwl.readLock().lock();
        try {
            System.out.println(Thread.currentThread().getName() + " be ready to read data!");
            Thread.sleep((long)(Math.random()*1000));
            System.out.println(Thread.currentThread().getName() + "have read data :" + data);           
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            rwl.readLock().unlock();
        }
    }
   
    public void put(Object data){

        rwl.writeLock().lock();
        try {
            System.out.println(Thread.currentThread().getName() + " be ready to write data!");                   
            Thread.sleep((long)(Math.random()*1000));
            this.data = data;       
            System.out.println(Thread.currentThread().getName() + " have write data: " + data);                   
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            rwl.writeLock().unlock();
        }
        
    
    }
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。