pausing a thread

S

sqad

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
 
G

Gordon Beaton

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

--
 
S

sqad

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
 
S

sqad

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.
 
L

Lothar Kimmeringer

sqad said:
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: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
S

sqad

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: (e-mail address removed)
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
 
G

Gordon Beaton

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

--
 
S

sqad

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top