"readwritelock example" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "readwritelock example" answered properly. Developers are finding an appropriate answer about readwritelock example related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like readwritelock example. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on readwritelock example.
readwritelock example
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class TestThread {
private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
private static String message = "a";
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new WriterA());
t1.setName("Writer A");
Thread t2 = new Thread(new WriterB());
t2.setName("Writer B");
Thread t3 = new Thread(new Reader());
t3.setName("Reader");
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
}
static class Reader implements Runnable {
public void run() {
if(lock.isWriteLocked()) {
System.out.println("Write Lock Present.");
}
lock.readLock().lock();
try {
Long duration = (long) (Math.random() * 10000);
System.out.println(Thread.currentThread().getName()
+ " Time Taken " + (duration / 1000) + " seconds.");
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() +": "+ message );
lock.readLock().unlock();
}
}
}
static class WriterA implements Runnable {
public void run() {
lock.writeLock().lock();
try {
Long duration = (long) (Math.random() * 10000);
System.out.println(Thread.currentThread().getName()
+ " Time Taken " + (duration / 1000) + " seconds.");
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
message = message.concat("a");
lock.writeLock().unlock();
}
}
}
static class WriterB implements Runnable {
public void run() {
lock.writeLock().lock();
try {
Long duration = (long) (Math.random() * 10000);
System.out.println(Thread.currentThread().getName()
+ " Time Taken " + (duration / 1000) + " seconds.");
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
message = message.concat("b");
lock.writeLock().unlock();
}
}
}
}
Source: www.tutorialspoint.com
All those coders who are working on the Whatever based application and are stuck on readwritelock example can get a collection of related answers to their query. Programmers need to enter their query on readwritelock example related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about readwritelock example for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of readwritelock example while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "readwritelock example". Visit this developer's friendly online web community, CodeProZone, and get your queries like readwritelock example resolved professionally and stay updated to the latest Whatever updates.