Results 1 to 6 of 6
Thread: java synchronisation
- 04-18-2011, 11:51 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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; }
- 04-19-2011, 12:01 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-19-2011, 06:23 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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
- 04-19-2011, 06:36 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,410
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-19-2011, 06:55 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
And how would that look not exact code but something for me to work on as im lost
- 04-19-2011, 07:21 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,410
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
process synchronisation:
By javanewuser in forum Threads and SynchronizationReplies: 0Last Post: 03-10-2011, 10:28 AM -
Synchronisation Issue?
By Jithu in forum New To JavaReplies: 1Last Post: 10-11-2010, 09:06 AM -
Synchronisation problem
By Joshy910 in forum SWT / JFaceReplies: 3Last Post: 08-12-2010, 03:52 PM -
thread synchronisation
By nabila.abdessaied in forum Threads and SynchronizationReplies: 0Last Post: 04-01-2009, 05:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks