1

I have a requirement in java where i want the thread to die and kill itself after specific period of time like after 1 min it has started to process. Does java provide a way for this?

One thing to add here is i am using ThreadPoolExecutor and submitting RUnnable objects to ThreadPoolExecutor to be executed in a queue. This is part of our framework and i cannot remove ThreadPoolExecutor. Given this how can i use ExecutorService?

user1241438
  • 1,533
  • 4
  • 17
  • 24
  • What are you trying to achieve with this thread? An IO task? – DankMemes Nov 29 '12 at 21:49
  • Not an IO task, this thread calls a thirdparty webservice and it is hanging for more than 15 mins. Hence i want to eliminate this thread – user1241438 Nov 29 '12 at 22:12
  • That is IO. Any streaming (like calling a webservice, that's connecting to a server) is IO. In that case, just close the Socket it's using. Not the most graceful solution, but it should work. – DankMemes Nov 30 '12 at 01:29

5 Answers5

2

It's the same problem like this. Please use ExecutorService to execute the Runnable

Community
  • 1
  • 1
bhuang3
  • 3,493
  • 2
  • 17
  • 17
  • 1
    `ExecutorService` tends to not behave very well when threads are created and destroyed too quickly. It seems that it frequently gets stuck shutting down threads. Are you aware of any other alternatives? –  Sep 30 '15 at 09:19
2

You must not just "kill thread", since thread can hold locks or other resources (like files). Instead add stop method to Runnable you execute in thread, which will set internal flag and check it in run method periodically. Like this:

class StopByPollingThread implements Runnable {
    private volatile boolean stopRequested;
    private volatile Thread thisThread;

    synchronized void start() {
        thisThread = new Thread(this);
        thisThread.start();
    }

    @Override
    public void run() {
        while (!stopRequested) {
            // do some stuff here
            // if stuff can block interruptibly (like calling Object.wait())
            // you'll need interrupt thread in stop() method as well
            // if stuff can block uninterruptibly (like reading from socket)
            // you'll need to close underlying socket to awake thread
            try {
                wait(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    synchronized void requestStop() {
        stopRequested = true;
        if (thisThread != null)
            thisThread.interrupt();
    }
}

Additionally, you may want to read Java concurrency in practice by Goetz.

Victor Sorokin
  • 11,878
  • 2
  • 35
  • 51
1

Use an ExecutorService with Future.get or invoke* style methods and that should give you what you want. However, you can always simply periodically check within the thread to see if the timeout has been reached so that the thread can exit gracefully.

Adrian Rodriguez
  • 3,232
  • 2
  • 24
  • 34
0

You can interrupt the thread, but depending on what you are doing in your worker thread, you need to manually check Thread.isInterrupted(). See this for details.

mbelow
  • 1,093
  • 6
  • 11
0

You can try Thread.interrupt() and in the thread itself check for interruption with Thread.isInterrupted(). Or, if your thread sleeps for a while, then just exit on InterruptedException. This really matters though on what you're doing. If you are waiting for an IO task, try closing the stream and exiting on the IOException thrown in the Thread.

DankMemes
  • 2,085
  • 2
  • 21
  • 30