===Implementation===
Algorithm for solving boat problem (high level):
If there are no children on island B Molokai or if there are no adults left on island AOahu, two children from island A Oahu will pilot over and one will return to AMolokai. If there is a child on island BMolokai, an adult will pilot over to island B Molokai and the child will bring the boat back to island AOahu. Once This process, carried to completion eventually results in all of the adults are all and children on island A, the children can begin to transfer themselves over to island BMolokai.
There are the following ;Shared state variables:{{c|<pre>lock L = new lockLock()boatIsland = Island.A
boatPilot = null
boatDeparting = false
boatLookingForChild = false
numChildrenOnB childCounter = 0numAdultsOnA adultCounterA = 0condition childrenWaitingOnA, adultsWaitingOnA, childrenWaitingOnB, adultsWaitingOnB, waitingToRide, waitingToPilot //separate condition variables all on lock LadultCounterB = 0sleepingChildren = 0waiters = 0
Each thread will attempt to acquire the lock pipe = new Communicator(which represents the boat). If the thread can performs one of its assigned tasks, then it executes it. Otherwise it will go to sleep.</pre>}}
Child Algorithm (with Pseudocode):Each child will begin running Condition variables, all on island A and try to acquire the lock.: {{c|childrenWaitingOnA}}, {{c|adultsWaitingOnA}}, {{c|childrenWaitingOnB}}, {{c|adultsWaitingOnB}}, {{c|waitingToRide}}, {{c|waitingToPilot}}
childItinerary () { myIsland = A while (true) { acquire LIn addition, each thread has a local variable to keep track of which island the person is currently on.
If the child is on island A and the boat is available for use, then it ;Basic ideaEach thread will ask for a passenger child and go attempt to sleep. Once a passenger child is found, acquire the pilot will be awoken by lock (which essentially represents control over the passenger child and pilot them to island Bboat). He now increments If the global counter numChildrenOnB. He wakes up the passenger child so that he thread can be moved to island B as a passenger and goes to sleep. The passenger, increments perform one of the numChildrenOnB counter and wakes up tasks fitting for the pilot. Once current state of the pilot child reawakensworld, he will speak to begin(), telling the begin thread the number of children currently on island Bit executes it. He then returns to island A and decrements numChildrenOnB. If the number of adults on island A (according to numAdultsOnA) is not 0Otherwise, he wakes up the adults waiting on A, puts himself it will go to sleep on the children waiting on A condition variable, corresponding to its current role and releases the lock (after wake). Otherwise, he simply releases the locklocation.
if the child is on island A and the boat is not departing { if the boat does not have a pilot and the boat is not looking for a passenger { //first move of pattern boatLookingForChild = true;Algorithm details boatPilot = CHILD; sleep and wait to pilot; pilot to B; wake up passenger; increment number of children Each child will begin running on B; boatIsland = B; boatLookingForChild = false; sleep Oahu and wait try to pilot;speakToBegin()decrement num of children on Bchild pilots to A boatIsland = A if number of adults on A != 0 {wake adults waiting on A; sleep and wait on island A; }release L; continue;}acquire the lock.
If the child is on island A Oahu and the boat is asking available for a passengeruse (i.e. {{c|<nowiki>boatLookingForChild == false</nowiki>}} and {{c|<nowiki>boatPilot == null</nowiki>}} and {{c|<nowiki>boatDeparting == false</nowiki>}}), it will assume the child will role of pilot and set the boat as departing{{c|boatLookingForChild}} variable to true, wake up the pilot child, and go then going to sleepon {{c|waitingToPilot}}. After Once a passenger child is found, the pilot child is will be awoken by the passenger child and pilotthem both to Molokai, he will transfer himself while the passenger goes to island B as a passenger and increment numChildrenOnBsleep on {{c|waitingToRide}}. He awakens then increments the {{c|childCounter}}, wakes up the pilot passenger child (so that he can pilot back ride to A with the boat)Molokai, puts the children waiting on B and goes back to sleep, and releases the lock.
else if (boatPilot == CHILD and boatLookingForChild) The passenger, upon arrival at Molokai, increments the {{ boatDeparting = true; wake a pilot; sleep c|childCounter}} and wait speaks to ride; child ride to B; increment {{c|begin()}} the number of children on B; myIsland = B; boatDeparting = false; wake a Molokai. The child passenger then wakes up the pilot; and goes to sleep on island B; release L; continue;{{c|childrenWaitingOnB}}. Once the pilot child reawakens, he returns to Oahu, decrementing {{c|childCounter}}before departure.
If Upon return to Oahu, if the number of adults on Oahu (according to {{c|adultsWaitingOnA}}) is not 0 and there is still a child is left on island B with Molokai to row the boat and back after the boat is not in useadult goes across (according to {{c|childCounter}}), he will the pilot wakes up the adults waiting on A, puts himself back to A sleep on {{c|childrenWaitingOnA}}, releases the lock after wake, and restarts the looop. Otherwise, he simply releases the lock and decrement restarts the counter numChildrenOnBloop.
} else if (myIsland == boatIsland == B The adults all begin on Oahu, try to acquire the lock, and !boatDeparting) each initially increment {decrement num of children on B child pilots to A; myIsland = boatIsland = A; boatPilot = null; release L; continue;{c|adultCounterA}}.
If none of these conditions are metthe adult is on Oahu, the boat is available for use, and there is at least one child is put on Molokai according to {{c|childCounter}}, the adult pilots to Molokai. Otherwise, he wakes a child on {{c|childrenWaitingOnA}} and goes to sleep as waiting on his respective island{{c|adultsWaitingOnA}}.
else If the adult does pilot across, he increments { if {c|adultCounterB}} upon arrival to Molokai, speaks to {{c|begin(myIsland == A)sleep }} the value of {{c|childCounter}}, wakes up a child from {{c|childrenWaitingOnB}} so that he can pilot the boat back to Oahu, and wait then puts himself to ride elsesleep on island B;{{c|adultsWaitingOnB}}. The adult is now done and will never be woken again.
Adult Algorithm (with Pseudocode):The adults all begin If a child is sleeping on Molokai on island A {{c|childrenWaitingOnB}} and gets woken by an adult, he will decrement {{c|childCounter}} and acquire pilot himself back to Oahu. Upon arrival, he releases the lock. The adults also increment a global counter numAdultsOnAand restarts the loop.
adultItineraryFor both adults and children, if the boat is busy or inaccessible as determined by the state of the {{c|boatPilot}}, {{c|boatDeparting}}, {{c|boatLookingForChild}}, and {{c|boatIsland}} variables given the person's current location, each will go back to sleep on the appropriate condition variable () i.e. {{ myIsland = A increment numAdultsOnA while(truec|childrenWaitingOnA}} for child threads on Oahu) { acquire L;.
If ;{{c|begin()}} behavior{{c|begin()}} creates the required number of child and adult is on island A, the boat is available for use threads and the last boat pilot was a child, the adult pilots to island B. He then wakes up one child waiting on island B so that he can pilot the boat back to Aforks them. The adult is now done with its task and goes to sleepThen, thus releasing the lock for other threads it begins to use. if {{c|listen(myIsland == boatIsland == A and !boatDeparting and boatPilot == CHILD and !boatLookingForChild) }} on the one shared Communicator ({ boatPilot = ADULT; adult pilots to B; boatIsland = myIsland = B; wake a child sleeping on B; sleep on B; continue;{c|pipe}} Otherwise the adult puts the adults on his respective island to sleep). else When {{ if c|begin(myIsland == A)sleep on A; elsesleep on B;} In begin()} receives a message, each time it checks the child threads speaks to it, begin() will check if "spoken" number of children on B is equal to as well as {{c|adultCounterB}} against the number of child spawned threads that it forked. Since all adults must be on B before number of children increases, this tells us that we are doneto see if everyone is across yet.
The reason that it does not check the value of {{c|childCounter}} directly is because by the time it receives the message, it is possible that a child has already piloted back to Oahu, thus decrementing the counter and preventing {{c|begin()}} from knowing the true number of children on Molokai.
===Testing===