Results 1 to 5 of 5
  1. #1
    bert682 is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default Concurrent trail example

    Hey guys,

    *Should be train example*

    Im doing a little assignment for a concurrent programming class. Its at the early stages and pretty basic I just cant seem to grasp it.

    The basic idea is that two trains are sharing a tunnel, the example im posting shows how it is done without concurrency, i.e. both trains will use the tunnel at the same time (single track tunnel).

    Code that follows is the 3 classes.
    Java Code:
    public class Train extends Thread
    {
       private static Tunnel tun = new Tunnel();  // only one tunnel
    
      
       private int id;   // id identifies each train
    
       public Train(int id) {
             // each train has a different identifing number
          this.id = id;
       }
    
     public void run() {
             // each train makes 3 loops
          for (int j=0; j<3; j++) {
          	System.out.println("Train "+ id +" is at station");
          	travel(100);
          	System.out.println("Train "+id +" is at the tunnel");
           [COLOR="Red"] //This where I need to pass "tun" a train parameter, but where is it?  Do I need to create it?[/COLOR]
          }
       }
    
       public int GetId () {
          return id;
       }
    
       public void travel(int time) {
          int limit = 500000*id;
          for (int j=0; j < time; j++) {
             for (int k=0; k < limit; k++) {double r = j / 2 * 2;}
          }
       }
    
    }
    Java Code:
    public class Tunnel
    {
       public void Tunnel () {}
    
       public  void useTunnel (Train t) {
          System.out.println("Train " + t.GetId() + " enters tunnel");
          t.travel(3); // train takes 3 time unit to go through tunnel
          System.out.println("Train " + t.GetId() + " exits tunnel");
    	}
    }
    Java Code:
    import java.io.*;
    public class Simulate
    {
       public static void main(String [] args) {
    
          // ***************************************
          // The code to create two trains (threads)
          // and start them executing
          // ***************************************
          Train t1 = new Train(1);
          Train t2 = new Train(2);
    
          t1.start();
          t2.start();
          
       }
    
    }
    I have highlighted my problem area in red. I know what I am supposed to do i just dont know how to use or properly create the required class type (train) parameter.

    Any ideas?

    Regards, Robert

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You will need to post your problem description as was given.

  3. #3
    bert682 is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    Im supposed to modify the code in the area that I have written in red. I am supposed to pass the tunnel class a train object. However I cant see a train object in the Train class. Do I create one or am I missing it?

    Regards, Robert.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You can pass "this". It's a train.

  5. #5
    bert682 is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    Legend!! Thanks so much. It makes sense to pass that now.

    Regards, Robert

Similar Threads

  1. Concurrent users to the web site
    By Chinnu in forum Java Servlet
    Replies: 3
    Last Post: 08-11-2009, 05:41 AM
  2. help with concurrent modification exception
    By jdgallag in forum New To Java
    Replies: 1
    Last Post: 11-30-2008, 09:19 PM
  3. Concurrent Applications
    By m@tqi in forum Advanced Java
    Replies: 1
    Last Post: 12-01-2007, 12:09 AM
  4. Concurrent Hierarchical State Machine 4.3
    By levent in forum Java Software
    Replies: 0
    Last Post: 08-03-2007, 04:44 PM
  5. Concurrent Versions System (CVS)
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-02-2007, 03:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •