Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Mutual Exclusion
- import java.util.concurrent.locks.*;
- public class ThreadDemo extends Thread {
- int threadNo;
- Lock lock = new ReentrantLock();
- ThreadDemo(int x) {
- this.threadNo = x;
- }
- @Override
- public void run() {
- try {
- lock.lock();
- System.out.println("Thread " + threadNo + " entered the critical section.");
- System.out.println("Thread " + threadNo + " out of the critical section.");
- } finally {
- lock.unlock();
- }
- }
- public static void main(String argsp[]) throws InterruptedException {
- ThreadDemo t1 = new ThreadDemo(1);
- ThreadDemo t2 = new ThreadDemo(2);
- ThreadDemo t3 = new ThreadDemo(3);
- t1.start();
- t2.start();
- t3.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement