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:

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:

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);
        }
    }

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'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.

Thanks so much! I makes sense to me so I will try that now and hopefully it works :) Thanks a million.

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...

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.)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.