Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashSet;
- import java.util.concurrent.Semaphore;
- public class CriminalTransport extends Thread{
- static Semaphore pEnter = null;
- static Semaphore cEnter = null;
- static Semaphore lock = null;
- static int police=0;
- static int criminal=0;
- static Semaphore canDrive = null;
- static Semaphore canExit = null;
- public static void init() {
- pEnter = new Semaphore(2);
- cEnter = new Semaphore(0);
- lock = new Semaphore(1);
- canExit = new Semaphore(0);
- canDrive = new Semaphore(0);
- }
- static class Policeman extends Thread{
- int threadID;
- public Policeman(int threadID) {
- this.threadID=threadID;
- }
- public void execute() throws InterruptedException {
- // waits until it is valid to enter the car
- pEnter.acquire();
- System.out.println("Policeman enters in the car");
- lock.acquire();
- police++;
- if(police == 2) {
- if(criminal == 0) {
- cEnter.release(2);
- }
- }
- if(police == 3 && criminal == 0) {
- cEnter.release();
- }
- if((police+criminal == 4 ) || (police==4 && criminal==0)) {
- canDrive.release(4);
- System.out.println("Start driving.");
- }
- lock.release();
- canDrive.acquire();
- Thread.sleep(10);
- lock.acquire();
- if(police+criminal == 4) {
- System.out.println("Arrived.");
- canExit.release(4);
- }
- lock.release();
- canExit.acquire();
- System.out.println("Policeman exits from the car");
- lock.acquire();
- police--;
- if(police == 0 && criminal == 0) {
- pEnter.release(2);
- System.out.println("RELEASED 2 POLICEMAN");
- }
- lock.release();
- // when the four passengers are inside, one policeman prints the starting command
- // one policeman prints the this command to notice that everyone can exit
- // the exit from the car is allowed after the "Arrived." message is printed
- }
- public void run(){
- try {
- execute();
- }catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- static class Criminal extends Thread{
- int threadID;
- public Criminal(int threadID) {
- this.threadID=threadID;
- }
- public void execute() throws InterruptedException {
- // waits until it is valid to enter the car
- cEnter.acquire();
- System.out.println("Criminal enters in the car");
- lock.acquire();
- criminal++;
- lock.release();
- canDrive.acquire();
- Thread.sleep(10);
- canExit.acquire();
- // the exit from the car is allowed after the "Arrived." message is printed
- System.out.println("Criminal exits from the car");
- lock.acquire();
- criminal--;
- lock.release();
- }
- public void run() {
- try {
- execute();
- }catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) throws InterruptedException {
- HashSet<Thread> threads = new HashSet<Thread>();
- for (int i = 0; i < 16; i++) {
- Policeman red = new Policeman(i);
- threads.add(red);
- Criminal green = new Criminal(i);
- threads.add(green);
- }
- // run all threads in background
- init();
- for(Thread p : threads) {
- p.start();
- }
- // after all of them are started, wait each of them to finish for maximum 1_000 ms
- for(Thread p : threads) {
- p.join(1000);
- }
- // for each thread, terminate it if it is not finished
- for(Thread p : threads) {
- if(p.isAlive()) {
- p.interrupt();
- System.out.println("Possible Deadlock!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement