This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Predict the output and why is it what it is ? - Thread related Q
Anupreet Arora
Ranch Hand
Joined: Jun 17, 2003
Posts: 81
posted
0
----------------------------------------------------- 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
posted
0
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. <br />- What truth? <br />- That there is no spoon!!!
S. Sumukh
Ranch Hand
Joined: Aug 27, 2003
Posts: 31
posted
0
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
posted
0
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
posted
0
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
posted
0
I'm using JDK1.4 and the output i get is 1.
Inja Bory
Greenhorn
Joined: Sep 01, 2003
Posts: 1
posted
0
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
posted
0
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
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.