• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting rid of infinite looping  RSS feed

 
Arun Sanker
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Have a situation in which I have a function called "Test()"as shown below.It has to wait until a flag becomes "1".
So Iam running it in an infinite loop until the flag becomes "1".

The "Test()" function is running in one thread.

Have another function called "eventListener"(See Below), which changes the flag to "1". This function gets called from another thread.


This works....

But I want to get rid of this infinite looping mechanisam because it is running the processor time...
Don't want to use the "sleep" or "join" stuffs to make the thread sleep..

Is there any elegant way of handling this. May be like some mutexes.....

Would be nice if someone could show what should be done in the following code to achieve that....

Thanks

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is precisely the situation where you use "wait()" and "notify()". I think you should make a cup of tea, sit down, and read through all of this tutorial on threading in Java. You will be glad you did!
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just what Object.wait, Object.notify and Object.notifyAll are created for.

Check out this section of the Java tutorial for more info.

Wow, just what Ernest said. I'm even pointing to roughly the same part of the Java tutorial, just a bit farther.
[ July 23, 2008: Message edited by: Rob Prime ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!