Code example for ReadWriteLock

Methods: sleep, writeLock

0
	private int calculatedValue; 
	private ReadWriteLock lock = new ReentrantReadWriteLock(); 
 
	public synchronized void calculate(int value) 
	{ 
		lock.writeLock().lock(); 
		try  
		{ 
			this.value = value; 
			this.calculatedValue = doSomeLongCalulation(value); 
		} finally 
		{ 
			lock.writeLock().unlock(); 
		} 
	} 
 
	private int  doSomeLongCalulation(int value) 
	{ 
		//System.out.print("Long Calculation"); 
		try  
		{			 
			Thread.sleep(3000); 
		} catch (InterruptedException e) { 
 
		} 
		return value+10;		 
	}