Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment: Write a multithreaded Java program that uses either semaphores or Ja

ID: 3927687 • Letter: A

Question

Assignment: Write a multithreaded Java program that uses either semaphores or Java synchronization to control access to a one-lane bridge. One thread should simulate eastbound vehicles and another thread should simulate westbound vehicles. The vehicles do not have a reverse gear, so the bridge would become deadlocked if both an eastbound vehicle and a westbound vehicle were allowed to drive onto the bridge at the same time. Therefore, mutual exclusion must be enforced. Your solution should avoid both deadlock and starvation (e.g. the bridge being monopolized by westbound vehicles while the eastbound vehicles never get to cross). Vehicles traveling in either direction should wait (sleep) for some amount of time, then attempt to cross. Once a vehicle is on the bridge, it should sleep for some amount of time to simulate how long it takes to drive across the bridge. Output a message when each vehicle drives onto the bridge and another message when that vehicle has completed the crossing. Simulate several vehicles traveling in each direction.

Explanation / Answer

public class baboon{



                public static boolean eastLock = false;


                public static boolean westLock = false;


                public static int>




                public static void main(String [] args)


                {


                                BaboonEast bEast = new BaboonEast();


                                BaboonWest bWest = new BaboonWest();


                                                               


                                for (int i=1; i<=10; i++)


                                {


                                                new Thread(bEast, "E: "+i).start();


                                                new Thread(bWest, "W: "+i).start();


                                               


                                                //bEast.start();


                                }//for


                               


                }//main


}//class



               


class BaboonEast extends Thread


{


                public void run()


                {


                                while (baboon.westLock == true)


                                {


                                                System.out.println(Thread.currentThread().getName() + " waiting");


                                                try


                                                {


                                                                sleep(50);


                                                } catch (InterruptedException ex) {}


                                }//while


                               


                                //baboon.eastLock = true; //stops west baboons entering


                                baboon.onBranch ++;


                                baboon.westLock = true;


                                baboon.eastLock = true;


                                System.out.println("Baboon " + Thread.currentThread().getName() + " is entering");


                                try


                                {


                                                sleep(100);


                                } catch (InterruptedException ex) {}


                                baboon.onBranch --;


                                System.out.println ("Baboon " + Thread.currentThread().getName() + " has crossed");


                                if (baboon.onBranch <= 0)


                                {


                                                baboon.eastLock = false;


                                               


                                               


                                               


                                                baboon.westLock = false;


                                               


                                               


                                }


                                //System.out.println(baboon.onBranch);


                }//run


}//class



               


class BaboonWest extends Thread


{


                public void run()


                {


                                while (baboon.eastLock == true)


                                {


                                                System.out.println(Thread.currentThread().getName() + " waiting");


                                                try


                                                {


                                                                sleep(50);


                                                } catch (InterruptedException ex) {}


                                }//while


                               


                                baboon.westLock = true; //stops east baboons entering


                                baboon.onBranch ++;


                                System.out.println("Baboon " + Thread.currentThread().getName() + " is entering");


                                baboon.westLock = true;


                                baboon.eastLock = true;


                                try


                                {


                                                sleep(100);


                                } catch (InterruptedException ex) {}


                               


                                baboon.onBranch --;


                                System.out.println ("Baboon " + Thread.currentThread().getName() + " has crossed");


                                if (baboon.onBranch <= 0)


                                {


                                                baboon.westLock = false;


                                               


                                               


                                               


                                                                baboon.eastLock = false;


                                               


                                               


                                }


                                //System.out.println(baboon.onBranch);


                }//run


}//class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote