Confused with threads-help with a homework

Thread Solved

Join Date: Jan 2007
Posts: 14
Reputation: Magda is an unknown quantity at this point 
Solved Threads: 0
Magda's Avatar
Magda Magda is offline Offline
Newbie Poster

Confused with threads-help with a homework

 
0
  #1
May 14th, 2009
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:
  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Confused with threads-help with a homework

 
0
  #2
May 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 14
Reputation: Magda is an unknown quantity at this point 
Solved Threads: 0
Magda's Avatar
Magda Magda is offline Offline
Newbie Poster

Re: Confused with threads-help with a homework

 
0
  #3
May 14th, 2009
Thanks so much! I makes sense to me so I will try that now and hopefully it works Thanks a million.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Confused with threads-help with a homework

 
0
  #4
May 14th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: Confused with threads-help with a homework

 
0
  #5
May 15th, 2009
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.)
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Confused with threads-help with a homework

 
0
  #6
May 15th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC