Results 1 to 6 of 6
  1. #1
    bangor_smith is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default java synchronisation

    Hi,

    I am bit stuck here, I have to have edit this code so that a train must wait in front of the tunnel lock for a train to come in the opposite direction to exit before it can enter the tunnel, i have to use java built in synchronisation condition. Before the try statement I have tried looking at while statements, if statements and still cant get it to give me the following output:

    Train 1 arrives
    Train 1 ascends
    Train 1 Exits Tunnel
    Train 2 Arrives
    train 2 descends
    train 2 exits tunnel
    train 3 and so on


    Any help would be greatful

    Java Code:
    public synchronized void useTunnelLock(Train train)
    {
    
    
    System.out.println(train + " " + train.getDirection());
    System.out.println(train + " exiting Tunnel");
    while(train.getDirection() == ASCENDING())
    {
    this.wait();
    }
    notifyAll();
    try
    {
    // occupy tunnel lock for 5s
    Thread.sleep(5000);
    }
    catch (InterruptedException ex)
    {
    ex.printStackTrace();
    }
    
    // swap direction of tunnel lock
    direction = (direction == ASCENDING)? DESCENDING : ASCENDING;
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im not terribly practiced with the use of threads, however; I believe that making the method synchronized makes it so a thread acquires a lock, and no other thread can access it until the thread is unlocked. If you synchronize the method it should make all other threads wait for the first thread to finish with the method before they can enter the method.

    What is your original code? What modifications have you made?

    As stated, I am not exceptionally experienced with threading and this is simply my understanding. Im sure other posters here will validify/falsify my advice.

  3. #3
    bangor_smith is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Correct in what you say, but seeing the thread has to wait till the thread coming in the opposite direction has arrived before it can go is the problem im having ie

    Thread 1 which is ascending starts and then exits the lock, at this point thread 3 or 4 which is descending should start, but without a statement stopping it, it is possible for thread 2 which is also ascending to start before thread 3 or 4 descends

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bangor_smith View Post
    Correct in what you say, but seeing the thread has to wait till the thread coming in the opposite direction has arrived before it can go is the problem im having ie

    Thread 1 which is ascending starts and then exits the lock, at this point thread 3 or 4 which is descending should start, but without a statement stopping it, it is possible for thread 2 which is also ascending to start before thread 3 or 4 descends
    The Tunnel is the shared resource here so the Trains should try to acquire a lock on the Tunnel. If all ascending (or descending) Trains can pass through the Tunnel without locking you effectively have two Tunnels: an ascending one and a descending one. An ascending Train has to acquire the descending Tunnel lock and vice versa.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    bangor_smith is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    And how would that look not exact code but something for me to work on as im lost

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bangor_smith View Post
    And how would that look not exact code but something for me to work on as im lost
    I'd use Semaphores (a class) for both directions or BlockingQueues (an interface). Read the API documentation for the java.util.concurrent package. Implementing wait() and notify() is done by those objects.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. process synchronisation:
    By javanewuser in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-10-2011, 10:28 AM
  2. Synchronisation Issue?
    By Jithu in forum New To Java
    Replies: 1
    Last Post: 10-11-2010, 09:06 AM
  3. Synchronisation problem
    By Joshy910 in forum SWT / JFace
    Replies: 3
    Last Post: 08-12-2010, 03:52 PM
  4. thread synchronisation
    By nabila.abdessaied in forum Threads and Synchronization
    Replies: 0
    Last Post: 04-01-2009, 05:11 PM

Tags for this Thread

Posting Permissions

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