pausing a thread

Discussion in 'Java' started by sqad, Oct 19, 2007.

  1. sqad

    sqad Guest

    Hi,

    I have a for loop:

    class TestObject {
    void runApp() {
    for (int i = 0 ; i < 4; i++ ) {
    // create a new thread and wait for execution to complete

    }
    }
    }

    Each iteration of the loop I need to create a new thread
    sqad, Oct 19, 2007
    #1
    1. Advertising

  2. On Fri, 19 Oct 2007 16:16:46 -0000, sqad wrote:
    > class TestObject {
    > void runApp() {
    > for (int i = 0 ; i < 4; i++ ) {
    > // create a new thread and wait for execution to complete
    >
    > }
    > }
    > }
    >
    > Each iteration of the loop I need to create a new thread


    Call join() after starting the thread to wait for it.

    But why do you need to start a thread, if you're just going to wait
    for it to complete?

    /gordon

    --
    Gordon Beaton, Oct 19, 2007
    #2
    1. Advertising

  3. sqad

    sqad Guest

    Sorry, accidently pressed enter before filling out the post.

    In each iteration, I need to create a new thread that executes, but
    the TestObject class should stop/wait for the execution of each
    iteration's thread and then go onto the next iteration.

    I passed a Thread.currentThread() as a reference into the Thread
    class, so it can say...parent_thread_reference.wait()...but TestObject
    still executes over all iterations and then runs the thread for each
    iteration, which is not what I want. :-(

    Does what I say make sense? Any ideas?

    /sqad
    sqad, Oct 19, 2007
    #3
  4. sqad

    sqad Guest

    On Oct 19, 9:20 am, Gordon Beaton <> wrote:
    > On Fri, 19 Oct 2007 16:16:46 -0000, sqad wrote:
    > > class TestObject {
    > > void runApp() {
    > > for (int i = 0 ; i < 4; i++ ) {
    > > // create a new thread and wait for execution to complete

    >
    > > }
    > > }
    > > }

    >
    > > Each iteration of the loop I need to create a new thread

    >
    > Call join() after starting the thread to wait for it.
    >
    > But why do you need to start a thread, if you're just going to wait
    > for it to complete?
    >
    > /gordon
    >
    > --


    The thread has a simple task. It runs a timer which monitors a file on
    the File System for modifications. If modifications are made then the
    thread is said to be "complete" and we can close the thread by
    'return;' within the run method. Then the next iteration can start and
    repeat the process for 'n' iterations.
    sqad, Oct 19, 2007
    #4
  5. sqad wrote:

    > The thread has a simple task. It runs a timer which monitors a file on
    > the File System for modifications. If modifications are made then the
    > thread is said to be "complete" and we can close the thread by
    > 'return;' within the run method. Then the next iteration can start and
    > repeat the process for 'n' iterations.


    I still don't see why you need a Thread for this:

    for (int i = 0; i < 4; i++){
    long initMod = file.getLastModified();
    try{
    Thread.sleep(1000);
    }
    catch(Exception e){
    break;
    }
    if (file.getLastModified() != initMod){
    // whatever
    }
    else{
    // whatever else
    }
    }

    Thread.sleep is static so you don't need to start a new instance
    of Thread for that.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
    Lothar Kimmeringer, Oct 19, 2007
    #5
  6. sqad

    sqad Guest

    On Oct 19, 9:33 am, Lothar Kimmeringer <>
    wrote:
    > sqad wrote:
    > > The thread has a simple task. It runs a timer which monitors a file on
    > > the File System for modifications. If modifications are made then the
    > > thread is said to be "complete" and we can close the thread by
    > > 'return;' within the run method. Then the next iteration can start and
    > > repeat the process for 'n' iterations.

    >
    > I still don't see why you need a Thread for this:
    >
    > for (int i = 0; i < 4; i++){
    > long initMod = file.getLastModified();
    > try{
    > Thread.sleep(1000);
    > }
    > catch(Exception e){
    > break;
    > }
    > if (file.getLastModified() != initMod){
    > // whatever
    > }
    > else{
    > // whatever else
    > }
    >
    > }
    >
    > Thread.sleep is static so you don't need to start a new instance
    > of Thread for that.
    >
    > Regards, Lothar
    > --
    > Lothar Kimmeringer E-Mail:
    > PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
    >
    > Always remember: The answer is forty-two, there can only be wrong
    > questions!


    Yes, but doesn't that mean the thread will wait a maximum of 1 second
    for the "task" to complete before going to the next iteration? What if
    the time needed to make the modifications can be dynamic? Could happen
    in 2 seconds...3 seconds later...or even 10 days later?

    Thanks for your reply.

    /sqad
    sqad, Oct 19, 2007
    #6
  7. On Fri, 19 Oct 2007 17:00:05 -0000, sqad wrote:
    > Yes, but doesn't that mean the thread will wait a maximum of 1
    > second for the "task" to complete before going to the next
    > iteration? What if the time needed to make the modifications can be
    > dynamic? Could happen in 2 seconds...3 seconds later...or even 10
    > days later?


    The point is that you use threads when you need to perform some action
    concurrent to the caller, i.e. so that the caller can continue with
    other work while the thread runs.

    If your caller simply waits for the thread to finish, it could have
    performed that work itself; the thread wasn't necessary.

    That said, the caller can wait for the thread to finish by calling
    t.join(), where t is a reference to the running thread.

    If you simply want to delay for some time, then call Thread.sleep(),
    just like that, without starting a thread.

    /gordon

    --
    Gordon Beaton, Oct 19, 2007
    #7
  8. sqad

    sqad Guest

    On Oct 19, 10:05 am, Gordon Beaton <> wrote:
    > On Fri, 19 Oct 2007 17:00:05 -0000, sqad wrote:
    > > Yes, but doesn't that mean the thread will wait a maximum of 1
    > > second for the "task" to complete before going to the next
    > > iteration? What if the time needed to make the modifications can be
    > > dynamic? Could happen in 2 seconds...3 seconds later...or even 10
    > > days later?

    >
    > The point is that you use threads when you need to perform some action
    > concurrent to the caller, i.e. so that the caller can continue with
    > other work while the thread runs.
    >
    > If your caller simply waits for the thread to finish, it could have
    > performed that work itself; the thread wasn't necessary.
    >
    > That said, the caller can wait for the thread to finish by calling
    > t.join(), where t is a reference to the running thread.
    >
    > If you simply want to delay for some time, then call Thread.sleep(),
    > just like that, without starting a thread.
    >
    > /gordon
    >
    > --


    Ahh understood and solved within the caller thread itself. I'll bake
    in a worker/producer solution at a later time so I can run concurrent
    threads. Thanks for the clarification, guys.

    /sqad
    sqad, Oct 19, 2007
    #8
    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. Bryan R. Meyer

    Pausing Threads From Events

    Bryan R. Meyer, Apr 27, 2004, in forum: Java
    Replies:
    16
    Views:
    1,466
    NOBODY
    Apr 30, 2004
  2. C-man
    Replies:
    2
    Views:
    5,209
  3. Sharp

    Pausing in Java

    Sharp, Jan 28, 2005, in forum: Java
    Replies:
    4
    Views:
    4,117
    Tony Morris
    Jan 28, 2005
  4. yccheok
    Replies:
    1
    Views:
    605
    Piotr Kobzda
    Mar 28, 2008
  5. Lucress Carol

    Pausing and continuing MFC Thread

    Lucress Carol, Sep 16, 2008, in forum: C++
    Replies:
    3
    Views:
    366
    terminator
    Sep 16, 2008
Loading...

Share This Page