Results 1 to 7 of 7
  1. #1
    Floiancu is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default Referencing object from another Thread

    My project is a simulation of the SJF algorithm. The "system" needs to generate new "programs" (with a certain duration and size) and manage them in terms of fitting them into the limited RAM, and "running" them.

    Now the problem: I need the program1.duration to decrease each second, so I made a new class Timer extends Thread, whose run function iterates every second, and whose only purpose is to decrement the duration of the program currently running.

    I have:
    Java Code:
    class Timer extends Thread{
    while(true){
                counter++;
                p_run.duration--;
                try {Thread.sleep(1000);} catch (InterruptedException ex) {}
            }
    }
    p_run is a Program object, created in the OperatingSystem class. I obviously cannot reference it from the Timer class (at least not the way I'm trying). What to change, what to do?
    Also, if there is another way to decrement an attribute of an object every second, I'm open to any suggestion.

    Thank you in advance!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Instead of decrementing your duration variable each second, why not just calculate and store the end time? The remaining duration can then be calculated whenever it's needed.

  3. #3
    Floiancu is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Ok, suppose I know the running program is 8 seconds long. How can I trigger it to stop? I would still have to check the duration attribute in the beginning, count the 8 seconds, then make it stop. How could I do that? If you have any ideas, I would appreciate a piece of code :)

    Thank you for your interest and response

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    What do you mean, "trigger it to stop"? Are your "programs" actually doing something, or do you just want to simulate them doing something?

    This is what I'm suggesting:

    Java Code:
    class Program {
    
         private int endTime;
    
         public Program(int duration) {
              endTime = System.currentTimeMillis() + duration;
         }
    
         public boolean isRunning() {
              return System.currentTimeMillis() < endTime;
         }
    
    }

  5. #5
    Floiancu is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    The Programs are Threads themselves. Their void run function generates a random code from 1 to 5 that the OS interprets as demands. So when their time is up I need them to Thread.stop().

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    I still don't see why you need to tell the thread to stop, if its run time is predetermined.

    Java Code:
    class Program implements Runnable {
    
         private int endTime;
    
         public Program(int duration) {
              endTime = System.currentTimeMillis() + duration;
         }
     
         public void run() {
              while(System.currentTimeMillis() < endTime) {
                   // do whatever
              }
         }
    }
    Also, you should never use Thread.stop(). The reasons are in the javadoc for Thread.

  7. #7
    Floiancu is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    YES!! It just dawned upon me this morning that I could use the system time to calculate the end time, but I still wouldn't have known how to implement it. The piece of code you gave me is exactly what I was looking for :)

    And about the Thread.stop() I know it's not advisable, but it's easier, and for the project I have it's not that bad. It's not like I'm writing code for an airplane :P

    Millions of thanks for that!

Similar Threads

  1. referencing files in .exe
    By minime12358 in forum Advanced Java
    Replies: 7
    Last Post: 07-26-2010, 09:28 PM
  2. Using weak referencing to gc listeners.
    By MartinR in forum New To Java
    Replies: 0
    Last Post: 05-30-2010, 12:37 AM
  3. Is object of my class in a thread or I messed up again?
    By atch in forum Threads and Synchronization
    Replies: 5
    Last Post: 02-09-2010, 09:51 AM
  4. Replies: 0
    Last Post: 01-28-2008, 08:02 AM
  5. how to access to an object from a thread
    By tamayo in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:24 AM

Tags for this Thread

Posting Permissions

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