| | |
Confused with threads-help with a homework
Thread Solved |
Hello,
I have been fighting with this assignment for almost 3 week now and it's almost ready besides one part that I simply don't understand. I would appreciate if someone could maybe translate it into "simple English" for me and maybe give me a little guidance. Basically we are writting a small train track simulation program ( I will include the two classes I have created later) and we are now asked to create method that will alternate in direction by presupposing that trains follow a timetable which prevents trains travelling in the same direction from colliding with each other.
Here's the question:
"The enterTrack() method should not allow a thread to proceed if:
•
the direction of travel is UP, and either
there is a DOWN thread currently using the track, or
the number of UP threads currently on the track is already greater than
two;
or
•
its mode is DOWN and any other threads are currently using the track.
exitTrack(TrainDirection dir) which records the fact that a
thread has finished using the track and wakes up any threads that are
waiting."
And my question is how do I go about referencing to the threads within these methods?
Here's the code:
The Train class:
And here's the Track class I am supposed to change:
Sorry for the long post
I am not expecting a solution, as mentioned I just need to know how I am supposed to use threads directly in this method.
Many thanks for your help and sorry if I am not clear enough in my post but Java still scares me
I have been fighting with this assignment for almost 3 week now and it's almost ready besides one part that I simply don't understand. I would appreciate if someone could maybe translate it into "simple English" for me and maybe give me a little guidance. Basically we are writting a small train track simulation program ( I will include the two classes I have created later) and we are now asked to create method that will alternate in direction by presupposing that trains follow a timetable which prevents trains travelling in the same direction from colliding with each other.
Here's the question:
"The enterTrack() method should not allow a thread to proceed if:
•
the direction of travel is UP, and either
there is a DOWN thread currently using the track, or
the number of UP threads currently on the track is already greater than
two;
or
•
its mode is DOWN and any other threads are currently using the track.
exitTrack(TrainDirection dir) which records the fact that a
thread has finished using the track and wakes up any threads that are
waiting."
And my question is how do I go about referencing to the threads within these methods?
Here's the code:
The Train class:
Java Syntax (Toggle Plain Text)
public class Train extends Thread { private Track track; private int trainId; private TrainDirection direction; public Train(TrainDirection dir, int id, Track tr) { direction = dir; trainId = id; track = tr; } public void run() { track.useTrack(direction, trainId); } }
And here's the Track class I am supposed to change:
Java Syntax (Toggle Plain Text)
public class Track { public Track() { } public void useTrack(TrainDirection dir, int id) { System.out.println("Train " + id + " entering track, going " + dir); traverse(); System.out.println("Train " + id + " leaving track, going " + dir); } public void enterTrack(TrainDirection dir) { } public synchronized void exitTrack(TrainDirection dir) { } /* You do not need to change this method */ private void traverse() { try { TimeUnit.MILLISECONDS.sleep(500); } catch (InterruptedException ie) { System.out.println(ie); } }
I am not expecting a solution, as mentioned I just need to know how I am supposed to use threads directly in this method.Many thanks for your help and sorry if I am not clear enough in my post but Java still scares me
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 146
I'd go for a simple solution - create int variables in the Thread class to hold the number of threads currently using the track in each direction. Increment the appropriate variable in useTrack and decrement it in exitTrack. Now you have the info necessary to perform the tests specified in your question.
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
Agree about the two ints for number of trains in UP vs DOWN direction. One thing, though-- did your tutor mention to you about thread-safety? If you have multiple threads accessing some data (here, multiple Trains accessing variables on the same Track), you need to use some mechanism to make sure that the various threads don't try and access the same data at the same time and "clash" with one another. One simple way of doing this is with the synchronized keyword-- I notice that one of your methods is synchronized, but really any methods that refer to the counters/variables on your Track should also be synchronized. (There are other ways of achieving thread-safety, but this is the simplest, and appears to be the one your example code is hinting at.)
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 146
That's a good point about thread safety. I didn't raise the issue in my previous post because the variables in question are ints, and assignments to ints in Java are atomic operations (guaranteed not to be interrupted by other thread). However, the potential for concurrency problems with the Track class in general is real, and synchronizing all the methods would definitely be a good idea.
![]() |
Similar Threads
- This or that? (Posting Games)
- Threads in dll (C)
- scheme newbie here!!! (Computer Science)
- How to find a through line in a C++ project? (C++)
- help with programming (C)
- help!..VerySimple web browser(display source code of web pages) (JSP)
- Can I change origional windows login name ??? (Windows tips 'n' tweaks)
Other Threads in the Java Forum
- Previous Thread: File System simulation
- Next Thread: unclosed string literal help!
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character chat class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list loops mac map method methods mobile netbeans newbie notdisplaying number online printf problem program programming project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor





