Thread behaviour

Discussion in 'Java' started by stixwix, Dec 15, 2005.

  1. stixwix

    stixwix Guest

    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
     
    stixwix, Dec 15, 2005
    #1
    1. Advertising

  2. stixwix

    O.L. Guest

    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
     
    O.L., Dec 15, 2005
    #2
    1. Advertising

  3. stixwix

    Viator Guest

    Please quote the source of Worker class also.
     
    Viator, Dec 15, 2005
    #3
  4. "stixwix" <> wrote in message
    news:...
    > 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.iviz.com/
     
    Matt Humphrey, Dec 15, 2005
    #4
  5. stixwix

    stixwix Guest

    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
     
    stixwix, Dec 16, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,586
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. pawo
    Replies:
    0
    Views:
    542
  3. Andy Chambers
    Replies:
    1
    Views:
    410
    Daniel Dyer
    May 14, 2007
  4. Just Another Victim of the Ambient Morality

    Strange behaviour with sleep() in a thread...

    Just Another Victim of the Ambient Morality, Dec 24, 2008, in forum: Ruby
    Replies:
    1
    Views:
    117
    Roger Pack
    Dec 27, 2008
  5. Xavier Noëlle

    [THREADS] Behaviour of Thread#stop?

    Xavier Noëlle, Dec 14, 2010, in forum: Ruby
    Replies:
    3
    Views:
    99
    Xavier Noëlle
    Dec 16, 2010
Loading...

Share This Page