Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Thread behaviour

Reply
Thread Tools

Thread behaviour

 
 
stixwix
Guest
Posts: n/a
 
      12-15-2005
Hi,

If I have a program which starts 100 threads in main:

for (int i = 0; i < 100; i++){
String name = Integer.toString(i);
Thread temp = new Thread(new Worker());
temp.setName(name);
temp.start();
}

where the Worker's run method just prints out the threads name, then in
this configuration I see about the last 50 threads print their message.

I initially thought that this is because the thread object referenced
by temp goes out of scope each time round the loop and so could be
garbage collected. But perhaps the fact that they are all in the same
thread group keeps them in scope?

If I use a CyclicBarrier and wait in main after the for loop (and force
each thread to wait at the end of its run method) then all threads are
seen to run.

I know this is a bit of a contrived example but am just interested
whether the thread objects are subject to GC the same as any other if
they can't be seen by main.

Thanks,
Andy

 
Reply With Quote
 
 
 
 
O.L.
Guest
Posts: n/a
 
      12-15-2005
Après mure réflexion, stixwix a écrit :
> where the Worker's run method just prints out the threads name, then in
> this configuration I see about the last 50 threads print their message.


Are you sure it's a good way to look at the threads activity ?
Try using an "int" counter, each thread increments the global static
counter, and then see what happens.

Bye

--
Olivier Ligny
Créateur web free-lance / www.cyber-tamtam.net


 
Reply With Quote
 
 
 
 
Viator
Guest
Posts: n/a
 
      12-15-2005
Please quote the source of Worker class also.

 
Reply With Quote
 
Matt Humphrey
Guest
Posts: n/a
 
      12-15-2005

"stixwix" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi,
>
> If I have a program which starts 100 threads in main:
>
> for (int i = 0; i < 100; i++){
> String name = Integer.toString(i);
> Thread temp = new Thread(new Worker());
> temp.setName(name);
> temp.start();
> }
>
> where the Worker's run method just prints out the threads name, then in
> this configuration I see about the last 50 threads print their message.
>
> I initially thought that this is because the thread object referenced
> by temp goes out of scope each time round the loop and so could be
> garbage collected. But perhaps the fact that they are all in the same
> thread group keeps them in scope?
>
> If I use a CyclicBarrier and wait in main after the for loop (and force
> each thread to wait at the end of its run method) then all threads are
> seen to run.
>
> I know this is a bit of a contrived example but am just interested
> whether the thread objects are subject to GC the same as any other if
> they can't be seen by main.


Threads are not eligible for GC simply because there are no references to
them from the main thread. If only some of the threads are running to
completion I would think it's because your main thread exits before they
finish and they are daemon threads. A java program will exit only when all
non-daemon threads have finished--running daemon threads are discarded. A
thread is a daemon thread by default if its parent is. Otherwise it can be
set to be daemon with setDaemon(true). The main thread is a non-daemon
thread.

Your message suggests that you're running non-daemon threads from the
non-daemon main thread, so I would expect all your threads to run to
completion (and they do so when I try it) so there may be something relevant
in the code you havn't shown.

When I run the code below (java Test daemon), I get a random number of
threads completing. If I run plain (java Test) I get all the threads
running to completion. As you found out above, if the main thread waits for
the threads to complete, they will all run to completion. (You may need to
tune the sleep parameter below to see how many get to run.)

public class Test {
public static final void main (String args []) {
Runnable task = new Runnable () {
public void run () {
for (int i = 0; i < 100; ++i) {
Thread t = new Thread (new Worker ());
t.setName ("Thread-" + i);
t.start ();
}
}
};

if ( (args.length > 0) && (args[0].equalsIgnoreCase ("daemon") ) ) {
Thread t = new Thread (task);
t.setDaemon (true);
t.start ();
try {Thread.sleep(100);} catch (InterruptedException ex)
{ ex.printStackTrace (); }

} else {
task.run ();
}
}
}
class Worker implements Runnable {
public void run () {
System.out.println (Thread.currentThread().getName());
}
}

Cheers,
Matt Humphrey http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
stixwix
Guest
Posts: n/a
 
      12-16-2005
Ah, apologies....the explanation lies in my not reading the output
properly!
I was running it as a JUnit test and saw the output in Eclipse:

Time: 0.016
OK (1 test)

half way through and assumed that this was the test starting point. Of
course, this got written because main completed and every thread did
indeed write to stdout.
Thanks for your help....
Andy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[THREADS] Behaviour of Thread#stop? Xavier Noëlle Ruby 3 12-16-2010 11:32 AM
Strange behaviour with sleep() in a thread... Just Another Victim of the Ambient Morality Ruby 1 12-27-2008 12:03 AM
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM



Advertisments