aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Predict the output and why is it what it is ?  - Thread related Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Predict the output and why is it what it is ?  - Thread related Q" Watch "Predict the output and why is it what it is ?  - Thread related Q" New topic
Author

Predict the output and why is it what it is ? - Thread related Q

Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
-----------------------------------------------------
class Test
{
public static void main(String[] args)
{
System.out.println(Thread.activeCount());
}
}
------------------------------------------------------
Predict the output and why is it what it is ?
Cheers
Anupreet
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
i believe the output is 1, because main is executed as a thread.
wait for other replies just to make sure.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
S. Sumukh
Ranch Hand

Joined: Aug 27, 2003
Posts: 31
I tried the above...code...
and o/p is 2 !!
can some one explain tht....
sumukh
Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
Hi,
Yes, the output to my surprise is 2 ! One of the active threads is the main thread. Which is the other one?
If I try to enumerate the active threads, as in the following code, I still get a null for the second thread! So why does the compiler say that the count is 2?
----------------------------------------------
class Test
{
public static void main(String[] args)
{
System.out.println(Thread.activeCount());
Thread tarray[] = new Thread[Thread.activeCount()];
Thread.enumerate(tarray);
for(int i = 0; i<tarray.length; i++)
{
System.out.println(i+": "+tarray[i]);
}
}
}
-----------------------------------------------
The output here is :
2
0: Thread[main,5,main]
1: null
-----------------------------------------------
So if the second thread is null, why is the count there?
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
i don't know about you guys, but the output i get is always 1.
i compiled it several times... and each time the output is 1.
i am using JDK 1.4
Anitha Lingam
Ranch Hand

Joined: Apr 21, 2002
Posts: 38
I'm using JDK1.4 and the output i get is 1.
Inja Bory
Greenhorn

Joined: Sep 01, 2003
Posts: 1
When I'm using Blackdown-1.3.1-02a-FCS the output is 1, but with Blackdown-1.4.1-01 the output I get is 2.
Any ideas?
Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
Hi Guys!
It never came to my mind that the response could be virtual machine implemenation dependent. Thanks for the inputs.
I ran the code from command prompt using jdk1.4_1, the answer i get is 2. If I run the code in VisualAge For Java, which has jdk1.2 on an IBM virtual machine, the answer is 1.
Maybe the Java Pundits at the ranch might like to comment on the issue.
Regards,
Anupreet
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The JVM implementation includes a number of threads of its own; for example, the garbage collection machinery includes two threadsin many JVMs, although these are usually not in the same ThreadGroup as the main thread. If you start jdb on any Java program, type "run" and then type the command "threads" you'll see the initial set of threads your JVM uses. Here's 1.4.1_01 on Linux:

But anyway, yes, the answer will be JVM dependent.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Predict the output and why is it what it is ? - Thread related Q