Solved

Threads in vector

Posted on 2002-05-20
4
285 Views
Last Modified: 2013-12-14
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
Comment
Question by:chencc77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 7021320
...
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
 

Author Comment

by:chencc77
ID: 7022851
Thanks Venci75, i am trying on it before close this question. Thanks again for ur help.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7022972
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
 
LVL 92

Expert Comment

by:objects
ID: 7074242
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHPStorm debugging issues 1 73
use lov values 2 72
servlet example 17 59
netbeans can not run self signed app 8 13
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question