I've done a simple test, and code as below :
public class InterruptTest
{
public static class MyTask implements Runnable {
#Override
public void run() {
System.out.println("before sleep " + Thread.currentThread().isInterrupted());
System.out.println(Thread.currentThread());
Thread.currentThread().interrupt();
System.out.println("after sleep " + Thread.currentThread().isInterrupted());
}
}
public static void main(String[] str)
{
ExecutorService service = Executors.newFixedThreadPool(1);
// MyTask task1 = new MyTask();
Future<?> future1 = service.submit(new InterruptTest.MyTask());
Future<?> future2 = service.submit(new InterruptTest.MyTask());
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
System.out.println("interrupted;");
}
}
}
And the output is :
before sleep false
Thread[pool-1-thread-1,5,main]
after sleep true
**before sleep false** // line 4
Thread[pool-1-thread-1,5,main]
after sleep true
why line 4 still output false ? not true ? Cause there is only one thread in current pool, and it should have been interrupted in the first task, why it's still available (not interrupted) when the second task runs ?
Thanks in advance!
Another question is I modify the run function as below :
public static class MyTask implements Runnable {
#Override
public void run() {
System.out.println("before sleep " + Thread.currentThread().isInterrupted());
System.out.println(Thread.currentThread());
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
System.out.println("interrupted;");
System.out.println("after sleep " + Thread.currentThread().isInterrupted());
System.out.println(Thread.currentThread());
}
}
}
And the output of one task is :
before sleep false
Thread[pool-1-thread-1,5,main]
interrupted;
after sleep false
The task should be waked up from sleep by thread.interrupt. But when I use Thread.currentThread().isInterrupted() to check it, it still return false.
Will sleep() eat the interrupt state ??
Looking at the source code of ThreadPoolExecutor, there is a private method called clearInterruptsForTaskRun documented as:
Ensures that unless the pool is stopping, the current thread does not have its interrupt set
Related
I've got simple test code:
public static void main(String[] args) {
CompletionService<Integer> cs = new ExecutorCompletionService<>(Executors.newCachedThreadPool());
cs.submit(new Callable<Integer>() {
public Integer call(){
try{
Thread.sleep(3000); // Just sleep and print
System.out.println("Sleeping thread: " + Thread.currentThread().getId());
}catch(InterruptedException e){
e.printStackTrace();
}
return 10;
}
});
try{
Future<Integer> fi = cs.take();
System.out.println(fi.get());
}catch(Exception e){
e.printStackTrace();
}
}
I run it, sleep 3 seconds, and prints
Sleeping thread: 14
10
But then it hangs there, the program doesn't end.
What's happening, how to make it finish?
As mentioned in the comments by tgdavies, your program will exit after +/- 60 seconds, because that is the default timeout for a thread without tasks in an ExecutorService created by Executors.newCachedThreadPool().
If you don't want to wait for 60 seconds, you should shutdown the executor service after you're done submitting tasks.
For example:
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
try {
CompletionService<Integer> cs = new ExecutorCompletionService<>(executorService);
cs.submit(new Callable<Integer>() {
public Integer call() {
try {
Thread.sleep(3000); // Just sleep and print
System.out.println("Sleeping thread: " + Thread.currentThread().getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
return 10;
}
});
try {
Future<Integer> fi = cs.take();
System.out.println(fi.get());
} catch (Exception e) {
e.printStackTrace();
}
} finally {
executorService.shutdown();
}
}
Alternatively, configure the executor service to create daemon threads using a custom ThreadFactory. Only do this if it is not a problem that an executor service that is doing actual work gets "killed" when there are no more normal (non-daemon) threads.
it's a simple java thread.
there are 2 threads, and it should be printed every 5 second.
there's no error.
but i just can't run.
please help me finding out what's wrong...
class MyThread extends Thread {
String message;
int delay;
public MyThread(String s, int d) {
message = s;
delay = d;
}
public void run() {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
System.out.println(message + "+" + delay );
}
}
}
public class applet {
public static void main(String[] args){
MyThread mt1, mt2;
mt1 = new MyThread("Hello", 5000);
mt2 = new MyThread("Bye ", 10000);
mt1.start();
mt2.start();
}
}
You are printing ONLY ONCE if an exception occurs...
Modify the run method to:
public void run() {
try {
Thread.sleep(delay);
System.out.println(message + "+" + delay );
} catch (InterruptedException e) {
System.err.println("error here" );
}
}
There are actually 2 mistakes in your program:
1. Print your message in the normal flow
So far you only print your message when you get an InterruptedException which is an exception that is thrown when your thread has been interrupted while sleeping. So instead of printing your message in the catch block, you are supposed to print it after the sleep as next:
try {
Thread.sleep(delay);
System.out.println(message + "+" + delay );
} catch (InterruptedException e) {
// Re-set the interrupted flag
Thread.currentThread().interrupt();
}
2. Add an infinite loop
As you wish to print your message every 5 seconds, you need to call sleep in an infinite loop otherwise your thread will print it only once and die, so the code should be at the end:
try {
while (true) {
Thread.sleep(delay);
System.out.printf("%s+%d%n", message, delay);
}
} catch (InterruptedException e) {
// Re-set the interrupted flag
Thread.currentThread().interrupt();
}
3. Bonus
Alternatively, you can use a ScheduledExecutorService to schedule your tasks with a fixed rate using the method scheduleAtFixedRate, your code would then be:
// Create a ScheduledExecutorService of 2 threads
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
// Schedule my task every 5 seconds starting in 5 seconds
executor.scheduleAtFixedRate(new MyTask("Hello", 5000), 5L, 5L, TimeUnit.SECONDS);
// Schedule my task every 10 seconds starting in 10 seconds
executor.scheduleAtFixedRate(new MyTask("Bye ", 10000), 10L, 10L, TimeUnit.SECONDS);
The class MyTask would simply be:
class MyTask implements Runnable {
String message;
int delay;
public MyTask(String s, int d) {
message = s;
delay = d;
}
public void run() {
System.out.printf("%s+%d%n", message, delay);
}
}
I have following sample created to mimic the situation i am encountering related to ExecutionService shutdown process. It seems that it terminates only one thread out of 3 something... and i get error messages on tomcat server.
public class Test {
static final ExecutorService threadExecutor = Executors.newCachedThreadPool();
static Runnable getTask(final String name) {
return new Thread() {
#Override
public void run() {
this.setName("Thread-" + name);
while (true) {
try {
System.out.println(name + " running...[" + this.getName() + "]");
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("InterruptedException..." + this.getName());
throw new Exception(e);
}
}
}
};
}
public static void main(String... strings) {
threadExecutor.submit(getTask("Task-1"));
threadExecutor.submit(getTask("Task-2"));
threadExecutor.submit(getTask("Task-3"));
//--
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
ThreadPoolExecutor tpe = (ThreadPoolExecutor) threadExecutor;
System.out.println("Active Threads=====>" + tpe.getActiveCount());
tpe.shutdown();
try {
if (!threadExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS)) {
System.out.println("Executor did not terminate in the specified time.");
List<Runnable> droppedTasks = tpe.shutdownNow();
System.out.println("Shutdown thread pool forecibly. " + droppedTasks.size() + " tasks will not be executed.");
}
System.out.println("Active Threads=====>" + tpe.getActiveCount());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
shutdown() initiates the shutdown process within the thread pool but allows current running tasks to finish. In your example the task does not finish because of while(true).
shutdownNow() initiates the shutdown and also interrupts the currently running threads. But again your task is handling that interrupted exception and running the while(true) loop.
I think you can simply share a common boolean between your tasks and caller code from where you are calling the threadPoolExecuror.shutdown(). Use that boolean in task instead of while(true).
What the right way to interrupt executor's thread?
I've got this:
Thread class with name Worker with method:
public void run() {
while(!(Thread.currentThread().isInterrupted()){
System.out.println("work " + Thread.currentThread().getName() + ":" + Thread.currentThread().isInterrupted());
}
}
And main class with:
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
Worker worker = new Worker();
executorService.execute(worker);
I try to call worker.interrupt(); or executorService.shutdownNow(); but my thread goes on and isInterrupted() is false.
Can you post all the relevant code? Based on the information you have given, I can't reproduce the behaviour you describe. See below a SSCCE that works as expected - output:
work pool-1-thread-1:false
work pool-1-thread-1:false
work pool-1-thread-1:false
....
Thread has been interrupted
Code:
public class Test {
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(1);
Worker worker = new Worker();
executorService.execute(worker);
executorService.shutdownNow();
}
public static class Worker extends Thread {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("work " + Thread.currentThread().getName() + ":" + Thread.currentThread().isInterrupted());
}
System.out.println("Thread has been interrupted");
}
}
}
Sorry if the question is quite simple. I am a beginner.
I have to create thread that calulates something, while the first thread works the other one have to measure if the first thread calculate the function in specified time. If not, it has to throw exception. Else it returns the answer.
I'd take the java.util.concurrent components - simple example
public void myMethod() {
// select some executor strategy
ExecutorService executor = Executors.newFixedThreadPool(1);
Future f = executor.submit(new Runnable() {
#Override
public void run() {
heresTheMethodToBeExecuted();
}
});
try {
f.get(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// do something clever
} catch (ExecutionException e) {
// do something clever
} catch (TimeoutException e) {
// do something clever
}
}
Have your thread notify a synchronization object when it is done and have your other thread wait x number of milliseconds for it to finish.
public class Main {
private static final Object mThreadLock = new Object();
static class DoTaskThread extends Thread {
public void run() {
try {
int wait = new Random().nextInt(10000);
System.out.println("Waiting " + wait + " ms");
Thread.sleep(wait);
} catch (InterruptedException ex) {
}
synchronized (mThreadLock) {
mThreadLock.notifyAll();
}
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
synchronized (mThreadLock) {
DoTaskThread thread = new DoTaskThread();
thread.start();
try {
// Only wait 2 seconds for the thread to finish
mThreadLock.wait(2000);
} catch (InterruptedException ex) {
}
if (thread.isAlive()) {
throw new RuntimeException("thread took too long");
} else {
System.out.println("Thread finished in time");
}
}
}
}
join is a lot simpler than using a lock.
join (millis)
Waits at most millis milliseconds
for this thread to die. A timeout of 0
means to wait forever.
Example code:
Thread calcThread = new Thread(new Runnable(){
#Override
public void run() {
//some calculation
}
});
calcThread.start();
//wait at most 2secs for the calcThread to finish.
calcThread.join(2000);
//throw an exception if the calcThread hasn't completed.
if(calcThread.isAlive()){
throw new SomeException("calcThread is still running!");
}
Have a look at http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ExecutorService.html#awaitTermination(long,%20java.util.concurrent.TimeUnit) which allows you to handle this without dealing with thread synchronization yourself.