sandeshMC

Mutual Exclusion

Apr 27th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. //Mutual Exclusion
  2. public class ThreadDemo extends Thread {
  3.  
  4.     int threadNo;
  5.  
  6.     ThreadDemo(int x) {
  7.         this.threadNo = x;
  8.     }
  9.  
  10.     public void run() {
  11.         System.out.println("Thread " + threadNo + " entered the critical section.");
  12.         System.out.println("Thread " + threadNo + " out of the critical section.");
  13.     }
  14.  
  15.     public static void main(String argsp[]) throws InterruptedException {
  16.         ThreadDemo t1 = new ThreadDemo(1);
  17.         ThreadDemo t2 = new ThreadDemo(2);
  18.         ThreadDemo t3 = new ThreadDemo(3);
  19.         t1.start();
  20.         t2.start();
  21.         t3.start();
  22.         t1.join();
  23.         t2.join();
  24.         t3.join();
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment