Thread reference?
for(int i = 0;i!=amountofthreads;i++){
new Thread(new ConnectionThread(i),new String("Thread"+i)).start();
}
May anybody teach me how to make a reference for threads created this way?
like once those threads started, i could do like (Whatever).getWhatever("Thread"+i).sleep(n)
?
thanks in advance
[583 byte] By [
Aldricha] at [2007-11-26 14:54:18]

> I tried putting it in a HashTable but it wont work.
> it returns objects, not Threads :((
>
> i tried..
>
> > Thread thread = WhatEverTable.getObject("Thread"+i);
>
>
> It wonk work
If this was true (it isn't) then the whole Collections API would be a sham.
You need a cast.
Thread thread = (Thread) yourhashtable.get(key);
Also do you really want to just have a reference to a Thread or do you want a reference to your specific Object?
Thanks! :)
ConnectionThread ct;
Hashtable hashtable = new Hashtable();
for(int i = 0;i!=20;i++) {
//new Thread(new ConnectionThread(i),new String("Thread"+i)).start();
hashtable.put("Thread"+i,new Thread(new ConnectionThread(i),new String("Thread"+i)));
Thread thread = (Thread) hashtable.get("Thread"+i);
thread.start();
}
Compiling 1 source file to C:\HServer_Backup_bakamasiranimonetJOKE\build\classes
Note: C:\HServer_Backup_bakamasiranimonetJOKE\src\hserver\Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
compile-single:
Is there a way to remove that? :D :D
> Thanks! :)
>
No problem.
>
> Is there a way to remove that? :D :D
Yes but it may be more than you want to know at present. That is a compiler warning (means the code has successfully compiled but the compiler is letting you know you may have written some buggy code) and you can have it go away by using Generics.
There is a whole tutorial on Generics which you can find here http://java.sun.com/docs/books/tutorial/java/generics/index.html