I have a bit of a problem. I have a java application and when it starts it needs to make around six maybe seven threads that just wait for some sort of event to occur (i.e. user presses a button). An example of how I make these threads is below. The problem is, I open my task manager only to see that all my four cpu cores are at 100% (up from around 20), as soon as I close my application everything returns to normal. I am still new to multi-threading and I know I am committing some sort of concurrency sin here but I would appreciate any insight on how to rectify this situation.
new Thread (new Runnable() { public void run()
{
while (true)
if (doSomeFunction)
{
myFunction();
doSomeFunction = false;
}
} } ).start();
// Somewhere else in the code
doSomeFunction = true;
I am thinking perhaps wait and notify would be the right approch to doing this?
EDIT: Just for clarification, this has nothing to do with powering swing components. Instead this does events based on a script, I don't want certain script functions to block but instead return immediately while finishing the task in the background.