Would it be better to interrupt() an idle thread rather than wait()/notify()?

thelostmachine :

I have a Manager that manages a couple of FileSystems (my own abstraction). It's possible that one of these FileSystems receives no data for awhile, so I'd like to idle it. Whenever data is finally received by the Manager, it should awaken the respective idle FileSystem. Right now I'm using wait() and notify() and I get the results I want.

However, the whole point of idling a FileSystem was to consume less resources. I was wondering if it may be better to just interrupt() the Thread then create a new one when it's time to activate a FileSystem (could take ten minutes or more for new data to come in)? Wouldn't waiting still hog resources?

Here's the setup of my Runnable.

public class FileSystem implements Runnable
{
    public FileSystem()
    {
        // do stuff
        startTimer()
    }

    @Override
    public void run()
    {
        while (!Thread.currentThread().isInterrupted())
        {
            // do stuff
        }
    }

    //public void startTimer()
    //{
    //    new Thread(new Runnable()
    //    {
    //        @Override
    //        public void run()
    //        {
    //            // do something
    //        }
    //    }).start();
    //}

    // other methods
}
Aman Goyal :

It depends. If read operation create lot of resources and happens very frequently then better for waiting for file. But if read thread is light and sits idle a lot, then better create new thread.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=82579&siteId=1