Try ReentrantReadWriteLock - Java Thread

Java examples for Thread:Lock

Description

Try ReentrantReadWriteLock

Demo Code


import java.util.Date;
import java.util.concurrent.locks.ReentrantReadWriteLock;


public class ReentrantReadWriteLockTest
{
    //from  w w w . j  a  v a  2  s .  c  o m
    private static class LoggerFactory
    {
        
    }
    
    private static class StubTest
    {
        private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        
        private LoggerFactory loggerFactory;
        
        public LoggerFactory getLoggerFactory()
        {
            try
            {
                lock.readLock().lock();
                System.out.println(Thread.currentThread().getName() + "Get-before sleep." + (new Date()));
                Thread.currentThread().sleep(1000l);
                System.out.println(Thread.currentThread().getName() + "Get-after sleep." + (new Date()));
                return loggerFactory;
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            finally
            {
                lock.readLock().unlock();
            }
            return null;
        }

        public void setLoggerFactory(LoggerFactory loggerFactory)
        {
            
            try
            {
                lock.writeLock().lock();
                this.loggerFactory = loggerFactory;
                System.out.println(Thread.currentThread().getName() + "Set-before sleep." + (new Date()));
                Thread.currentThread().sleep(3000l);
                System.out.println(Thread.currentThread().getName() + "Set-after sleep." + (new Date()));
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            finally
            {
                lock.writeLock().unlock();
            }
        }

        public void test()
        {
            LoggerFactory loggerFactory = new LoggerFactory();
            this.getLoggerFactory();
            this.setLoggerFactory(loggerFactory);
            this.getLoggerFactory();
            this.setLoggerFactory(loggerFactory);
        }
        public void test1()
        {
            this.getLoggerFactory();
            this.getLoggerFactory();
            this.getLoggerFactory();
        }
    }

    private void testSharedLock()
    {
        final StubTest stubTest = new StubTest();
        Thread t1 = new Thread()
        {
            
            @Override
            public void run()
            {
                stubTest.test();
            }
            
        };
        t1.setName("t1-");
        Thread t2 = new Thread()
        {
            
            @Override
            public void run()
            {
                stubTest.test1();
            }
            
        };
        t2.setName("t2-");
        t1.start();
        t2.start();
        
    }
    
    public static void main(String[] args)
    {
        ReentrantReadWriteLockTest test = new ReentrantReadWriteLockTest();
        test.testSharedLock();
    }
    
}

Related Tutorials