Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

pausing a thread

Thread view: 
sqad - 19 Oct 2007 17:16 GMT
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
Gordon Beaton - 19 Oct 2007 17:20 GMT
> class TestObject {
>  void runApp() {
[quoted text clipped - 6 lines]
>
> 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

--
sqad - 19 Oct 2007 17:27 GMT
> > class TestObject {
> >  void runApp() {
[quoted text clipped - 15 lines]
>
> --

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.
Lothar Kimmeringer - 19 Oct 2007 17:33 GMT
> 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
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

sqad - 19 Oct 2007 18:00 GMT
On Oct 19, 9:33 am, Lothar Kimmeringer <news200...@kimmeringer.de>
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
[quoted text clipped - 31 lines]
> 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
Gordon Beaton - 19 Oct 2007 18:05 GMT
> 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

--
sqad - 19 Oct 2007 21:34 GMT
> > 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
[quoted text clipped - 18 lines]
>
> --

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 - 19 Oct 2007 17:23 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.