Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt on the enumerate() method  RSS feed

 
Arunan Ramanathan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thread1.start();
thread2.start();

Thread[] ta = new Thread[Thread.activeCount()];

int n = Thread.enumerate(ta);

for (int i=0;i<n;i++) {
System.out.println("Thread " +i+" is "+ ta[i].getName());
}

1. In the above code i started two threads
2. I created an array of threads.
3. Passed that array in to the enumerate method.

The result is :

Thread 0 is main
Thread 1 is Joba thread
Thread 2 is Jobb thread

My doubt is :
1. Thread.activeCount() method will return an int.
2.Thread[] ta = new Thread[Thread.activeCount()]; this line will just create an array of new threads.
3.By iterating through the array how did we got the thread names on it

How this magic happened?>
 
Vijitha Kumara
Bartender
Posts: 3973
24
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's enumerate() method which copies every active threads in the current thread's thread group in to the given array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!