• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Threads in vector

I have 3 classes.

//Task.class as follow
import java.util.*;
import java.io.*;
import java.text.*;

public class Task extends Thread {
     boolean live = true;
     long sleepInterval;
     int cc = 0;
     public static Task Instance;
     public Task() {
          Instance = this;
          sleepInterval = 5000;
          start();
     }
     
     public void run() {
     
          while (live) {
               try {
                    sleep(sleepInterval);
               } catch (Exception e) {
                    System.out.println("Interrupted sleep:loadsubmit " + e);
                    }
          }
     }

     public void setSleepInterval()
     {
          this.interrupt();
          sleepInterval = 10000;
     }

     public void printText() {
          System.out.println("thread is alive " + this.isAlive());
     }
}

//END

2. UpdateTask.class will extends Thread and call Task.Instance.setSleepInterval to update the sleepInterval for specific Task.
2. Main.class contain a vector to add a numbers of Task.class.
My answer is how can i refer UpdateTask to the specific Task.Instance because every Task.class has the same Instance name (Task).

Thanks alot.

0
chencc77
Asked:
chencc77
  • 2
1 Solution
 
Venci75Commented:
...
private static Vector instances = new Vector();
public Task() {
  instances.addElement(this);
  sleepInterval = 5000;
  start();
}
public static Task getInstance(int i) {
  return (Task) instances.get(i);
}
...
   
now you can use :
Task.getInstance(2).setSleepInterval();
0
 
chencc77Author Commented:
Thanks Venci75, i am trying on it before close this question. Thanks again for ur help.
0
 
objectsCommented:
The only reason to have a global reference to your Threads is if you require all classes to have access to them. If you only require access from UpdateTask then simply pass your vector of Task instances to the UpdateTask ctor.

Vector threadList = new Vector(100);
for (int i=0; i<100; i++)
{
 Thread t = new Thread1();
 threadList.add(t);
}

UpdateTask updater = new UpdateTask(threadList);
0
 
objectsCommented:
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now