943,685 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 790
  • Java RSS
May 14th, 2009
0

Confused with threads-help with a homework

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  1. public class Train extends Thread
  2. {
  3. private Track track;
  4. private int trainId;
  5. private TrainDirection direction;
  6.  
  7. public Train(TrainDirection dir, int id, Track tr)
  8. {
  9. direction = dir;
  10. trainId = id;
  11. track = tr;
  12. }
  13.  
  14. public void run()
  15. {
  16. track.useTrack(direction, trainId);
  17. }
  18. }

And here's the Track class I am supposed to change:
Java Syntax (Toggle Plain Text)
  1. public class Track
  2. {
  3.  
  4.  
  5. public Track()
  6. {
  7.  
  8. }
  9.  
  10. public void useTrack(TrainDirection dir, int id)
  11. {
  12.  
  13. System.out.println("Train " + id + " entering track, going " + dir);
  14. traverse();
  15. System.out.println("Train " + id + " leaving track, going " + dir);
  16.  
  17. }
  18. public void enterTrack(TrainDirection dir)
  19. {
  20.  
  21. }
  22.  
  23. public synchronized void exitTrack(TrainDirection dir)
  24. {
  25.  
  26. }
  27.  
  28.  
  29.  
  30.  
  31. /* You do not need to change this method */
  32. private void traverse()
  33. {
  34. try
  35. {
  36. TimeUnit.MILLISECONDS.sleep(500);
  37. }
  38. catch (InterruptedException ie)
  39. {
  40. System.out.println(ie);
  41. }
  42. }
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
Similar Threads
Reputation Points: 23
Solved Threads: 0
Newbie Poster
Magda is offline Offline
14 posts
since Jan 2007
May 14th, 2009
0

Re: Confused with threads-help with a homework

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is offline Offline
5,767 posts
since Apr 2008
May 14th, 2009
0

Re: Confused with threads-help with a homework

Thanks so much! I makes sense to me so I will try that now and hopefully it works Thanks a million.
Reputation Points: 23
Solved Threads: 0
Newbie Poster
Magda is offline Offline
14 posts
since Jan 2007
May 14th, 2009
0

Re: Confused with threads-help with a homework

No problem. ps: got my method names a little confused - what I meant was put the increment/decrement in enterTrack & exitTrack, then call those from the appropriate places in useTrack. They're obviously there for a reason...
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is offline Offline
5,767 posts
since Apr 2008
May 15th, 2009
0

Re: Confused with threads-help with a homework

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.)
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
May 15th, 2009
0

Re: Confused with threads-help with a homework

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is offline Offline
5,767 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need advice on a future application
Next Thread in Java Forum Timeline: Difference between adding a node and creating a list of nodes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC