-1

How can I kill a thread if the time for him over or it return me the data?

  • how I start it
  • I need to pass a arg for him
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Sooo oo
  • 9
  • 4
  • 1
    How did you start the thread in question? – ernest_k May 08 '18 at 11:34
  • @JohnKugelman Not a duplicate. Modern Java conventions discourage you from creating `Thread` objects explicitly, so knowing how to kill one is not necessarily useful. `Thread` <> all kinds of threads. If you view this question as "How can I cancel an async task after a certain timeout?" (and [it's certain plausible that's what they wanted](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)), the advise in those answers is basically not helpful. – Michael May 08 '18 at 11:53
  • It's definitely a duplicate. Feel free to link more questions. – John Kugelman May 08 '18 at 11:55
  • I want to kill it if it not return data after 10 second – Sooo oo May 08 '18 at 11:57
  • There are 4 different questions (how to create a thread, how to pass an arg to a thread, how to kill a thread, how to get data from a thread) - all of them are too broad. Could you share the code you have written for this? What problems exactly do you have? – Andrew Tobilko May 08 '18 at 11:59
  • Yes I need to create a thread that has all the 4 options – Sooo oo May 08 '18 at 12:04
  • @JohnKugelman Once again, those are all specific to `java.lang.Thread` which you should not be using. The closest I can find is [this](https://stackoverflow.com/questions/24027314/how-can-i-cancel-a-task-after-a-timeout) but the answers aren't particularly great – Michael May 08 '18 at 12:05

1 Answers1

0

You can use Future.get with a timeout.

If the timeout is exceeded, you'll get a TimeoutException, which you can handle any way you like.

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Main
{
    private static int foo(final int arg)
    {
        /* Uncomment to see future get interrupted
        try
        {
            Thread.sleep(2000); 
        }
        catch (InterruptedException e) { } */

        return 1000 + arg;
    }

    public static void main(String[] args)
        throws ExecutionException, InterruptedException
    {
        final ExecutorService executor = Executors.newSingleThreadExecutor();
        final Future<Integer> future = executor.submit(() -> Main.foo(3));

        try
        {
            int result = future.get(1, TimeUnit.SECONDS);
            System.out.println(result);
        }
        catch (TimeoutException e)
        {
            future.cancel(true);
        }

        executor.shutdown();
    }
}
Michael
  • 41,989
  • 11
  • 82
  • 128
  • It's tell me "cannot resolve method 'submit'" – Sooo oo May 08 '18 at 11:52
  • I've added the imports in case you've imported the wrong thing. If you're not on Java 8, you'll need to convert the lambda expression to an anonymous `Callable`. – Michael May 08 '18 at 11:56