• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-----------------------------------------------------
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
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i believe the output is 1, because main is executed as a thread.
wait for other replies just to make sure.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the above...code...
and o/p is 2 !!
can some one explain tht....
sumukh
 
Anupreet Arora
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using JDK1.4 and the output i get is 1.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic