Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
atiqur rahman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How does the main method look like?
 
atiqur rahman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class TestThreads {
public static void main(String args[]) {
new TestT2().doIt();
}
}
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic