Results 1 to 2 of 2
  1. #1
    imadabh is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default 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);
    	}
    }

  2. #2
    imadabh is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Strange behaviour

    I am using Ubunutu 10.04 as my OS and jdk 1.6 to run the code.

Similar Threads

  1. Strange JVM behaviour
    By pjpr in forum Advanced Java
    Replies: 13
    Last Post: 01-03-2011, 07:39 PM
  2. Strange eclipse behaviour with new version
    By KingOfLions in forum Eclipse
    Replies: 0
    Last Post: 09-10-2009, 01:42 PM
  3. Strange behaviour in serialization
    By Wolverine in forum Networking
    Replies: 0
    Last Post: 05-23-2009, 12:03 PM
  4. AffinedTransform strange behaviour
    By Echilon in forum AWT / Swing
    Replies: 3
    Last Post: 12-11-2008, 09:58 AM
  5. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 09:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •