Results 1 to 2 of 2
Thread: Strange behaviour
- 05-11-2011, 04:11 PM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Strange behaviour
Hi all
I am trying to learn synchronization/multithreading. I have written a small snippet of code. After running for around 30 minutes, this code ends up in a deadlock. I am trying to figure out a reason. any help will be really appreciated. Thanks. Following is the code
Java Code:package rollerdemo; //Simulate a roller coaster, as in exercise 3.5 of Magee and Kramer. import java.util.concurrent.CyclicBarrier; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; //This thread deals with the passengers arriving at random intervals. class TurnstileBarrier implements Runnable { private ControlBarrier controlBarrier; private CyclicBarrier cyclicBarrier; TurnstileBarrier(ControlBarrier controlBarrier, CyclicBarrier cyclicBarrier) { this.controlBarrier = controlBarrier; this.cyclicBarrier = cyclicBarrier; } public void run(){ try { controlBarrier.welcomePassenger(); cyclicBarrier.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (BrokenBarrierException e) { Thread.currentThread().interrupt(); } finally { } } } class RollerBarrier { public static int NUMBER_OF_PASSENGERS_PER_CAR = 20; public static void main(String[] args) throws InterruptedException { final ControlBarrier controlBarrier = new ControlBarrier(); Runnable carBarrier = new Runnable() { public void run(){ controlBarrier.departCar(); } }; CyclicBarrier barrier = new CyclicBarrier(NUMBER_OF_PASSENGERS_PER_CAR, carBarrier); ExecutorService pool = Executors.newFixedThreadPool(NUMBER_OF_PASSENGERS_PER_CAR); while(true) pool.execute(new TurnstileBarrier(controlBarrier,barrier)); } } // The Control class represents the state of the controller, and the actions // which can be performed. class ControlBarrier { private long queued = 0; private long carsSent = 0; private long numberOfPassengersTravelledToday = 0; ControlBarrier() { queued = 0; carsSent = 0; numberOfPassengersTravelledToday = 0; } synchronized void welcomePassenger() { System.out.format("Welcoming Passenger %d%n", ++queued); ++numberOfPassengersTravelledToday; } synchronized void departCar() { queued = queued - RollerBarrier.NUMBER_OF_PASSENGERS_PER_CAR; System.out.format("The car %d is going now and the number of passengers travelled so far with us is %d%n", ++carsSent, numberOfPassengersTravelledToday); } }
- 05-11-2011, 04:31 PM #2
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Strange JVM behaviour
By pjpr in forum Advanced JavaReplies: 13Last Post: 01-03-2011, 08:39 PM -
Strange eclipse behaviour with new version
By KingOfLions in forum EclipseReplies: 0Last Post: 09-10-2009, 02:42 PM -
Strange behaviour in serialization
By Wolverine in forum NetworkingReplies: 0Last Post: 05-23-2009, 01:03 PM -
AffinedTransform strange behaviour
By Echilon in forum AWT / SwingReplies: 3Last Post: 12-11-2008, 10:58 AM -
Strange behaviour in swing
By cbalu in forum AWT / SwingReplies: 1Last Post: 05-23-2008, 10:23 PM
Bookmarks