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]
# 1
Well how do you normally keep a reference to objects?
cotton.ma at 2007-7-8 8:42:44 > top of Java-index,Java Essentials,Java Programming...
# 2
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
Aldricha at 2007-7-8 8:42:44 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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?

cotton.ma at 2007-7-8 8:42:44 > top of Java-index,Java Essentials,Java Programming...
# 4

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

Aldricha at 2007-7-8 8:42:44 > top of Java-index,Java Essentials,Java Programming...
# 5

> 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

cotton.ma at 2007-7-8 8:42:44 > top of Java-index,Java Essentials,Java Programming...
# 6
Thank you very much!! :)
Aldricha at 2007-7-8 8:42:45 > top of Java-index,Java Essentials,Java Programming...