File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

atiqur rahman
Greenhorn

Joined: Aug 30, 2003
Posts: 10
class T2 implements Runnable {
private int a;
public void run() {
System.out.println(a);
String s = Thread.currentThread().getName();
StringTokenizer st = new StringTokenizer(s);
a = Integer.parseInt(st.nextToken());
}
}
class TestT2 {
void doIt() {
T2 a = new T2();
Thread t1 = new Thread(a, "1");
Thread t2 = new Thread(a, "2");
Thread t3 = new Thread(a, "3");
t1.start();
t2.start();
t3.start();
System.out.println("======"+Thread.activeCount());
}
}
Ques:
When i run this class using textpad the result is 4
and when i run it by opening a dos shell in win98 mechine i got 5
Why??
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi,
How does the main method look like?


"Eppur si muove!"
atiqur rahman
Greenhorn

Joined: Aug 30, 2003
Posts: 10
public class TestThreads {
public static void main(String args[]) {
new TestT2().doIt();
}
}
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
I would expect always a 4 as a result (1 main thread + 3 new threads):
I played with the code as follows:

But, as expected, I can't find the 5th thread.
This I've done in a dos shell.
Gian Franco
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
The result will always be the same whether you compiled using textpad, DOS or any other IDE, that is four as Gian suggested.
I recommend you delete any files with the .class extension located in the directory you are compiling, then give it another shot and see what happens.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread