OnJava8-Examples/ui/InterruptableLongRunningTask.java

53 lines
1.5 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// ui/InterruptableLongRunningTask.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
// Long-running tasks in threads.
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.*;
import static onjava.SwingConsole.*;
2015-06-15 17:47:35 -07:00
class Task implements Runnable {
private static int counter = 0;
private final int id = counter++;
@Override
public void run() {
System.out.println(this + " started");
try {
TimeUnit.SECONDS.sleep(3);
} catch(InterruptedException e) {
System.out.println(this + " interrupted");
return;
}
System.out.println(this + " completed");
}
@Override
public String toString() { return "Task " + id; }
public long id() { return id; }
};
public class InterruptableLongRunningTask extends JFrame {
private JButton
b1 = new JButton("Start Long Running Task"),
b2 = new JButton("End Long Running Task");
ExecutorService executor =
Executors.newSingleThreadExecutor();
public InterruptableLongRunningTask() {
b1.addActionListener(e -> {
Task task = new Task();
executor.execute(task);
System.out.println(task + " added to the queue");
});
b2.addActionListener(e -> {
executor.shutdownNow(); // Heavy-handed
});
setLayout(new FlowLayout());
add(b1);
add(b2);
}
public static void main(String[] args) {
run(new InterruptableLongRunningTask(), 200, 150);
}
2015-09-07 11:44:36 -06:00
}