Results 1 to 9 of 9
  1. #1
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default How to know when a thread ends.

    Hi all,

    I have a multi-threaded program. This program, lets say spawns 10 threads. I would like to update a variable when each thread finishes its job.

    The reason why I am asking this, is because I would like to show something like progress bar (not really a progress bar, but display a precentage of job completion, while the program is running).

    I have this thaugh, and someone please correct me if I am wrong.

    I have the following classes

    - MainProgram.java
    - MainProgramThread.java
    - MainProgramProgressThread.java

    If MainProgram creates 10 instance of MainProgramThread class, I would like to decrement a counter (which has the maximum number of threads running)

    On the other hand, MainProgramProgressThread class will only display the progress percentage (this will be calculated) so long as the number of counter is greater than zero when this value reaches 0, then I would like the ProgressThread to stop.

    So, my question is how can I know when a thread ends?

    Or if there's another way of doing what I would like to do, I am open to ideas.

    Thanks in advance...

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Have the thread call a synchronized method before it exits.

  3. #3
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    Thanks once again Norm. It worked!

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    You can also use executor service ... which is bundled in java 5 .. it has feature of getting the success flag once the flag has finished processing.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm not exactly clear what you mean. Are you talking about use of a dummy flag, that's the word I use, to process a thread. Like this.

    Java Code:
    public void run() {
        while(true) {
            // Do the thread process here
        }
    }

  7. #7
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    actually it is based on de' lough concurrent package and is handy in managing thread pools with executors and worker threads.

  8. #8
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    and it has a getter method which returns the true and false for completion of thread.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. passing a value from parent thread to child thread
    By sachinj13 in forum Threads and Synchronization
    Replies: 7
    Last Post: 09-07-2008, 09:06 PM
  2. Does any file in an FTP server ends up in an HTTP server?
    By islamfunny in forum CLDC and MIDP
    Replies: 4
    Last Post: 08-15-2008, 04:30 PM
  3. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •