OnJava8-Examples/ui/InterruptableLongRunningCallable.java

55 lines
1.6 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// ui/InterruptableLongRunningCallable.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
// Using Callables for long-running tasks.
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.*;
import onjava.*;
import static onjava.SwingConsole.*;
2015-06-15 17:47:35 -07:00
class CallableTask extends Task
implements Callable<String> {
@Override
public String call() {
run();
return "Return value of " + this;
}
}
public class
InterruptableLongRunningCallable extends JFrame {
private JButton
b1 = new JButton("Start Long Running Task"),
b2 = new JButton("End Long Running Task"),
b3 = new JButton("Get results");
private TaskManager<String,CallableTask> manager =
new TaskManager<>();
public InterruptableLongRunningCallable() {
b1.addActionListener(e -> {
CallableTask task = new CallableTask();
manager.add(task);
System.out.println(task + " added to the queue");
});
b2.addActionListener(e -> {
for(String result : manager.purge())
System.out.println(result);
});
b3.addActionListener(e -> {
// Sample call to a Task method:
for(TaskItem<String,CallableTask> tt : manager)
tt.task.id(); // No cast required
for(String result : manager.getResults())
System.out.println(result);
});
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
}
public static void main(String[] args) {
run(new InterruptableLongRunningCallable(), 200, 150);
}
2015-09-07 11:44:36 -06:00
}