Java by API/java.util.concurrent/ScheduledThreadPoolExecutor
new ScheduledThreadPoolExecutor(int corePoolSize)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Main {
public static void main(String[] a){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 300);
PaintSurface canvas = new PaintSurface();
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
executor.scheduleAtFixedRate(canvas, 0L, 100L, TimeUnit.MILLISECONDS);
f.add(canvas);
f.setVisible(true);
}
}
class PaintSurface extends JComponent implements Runnable{
int i = 0;
public void run() {
repaint();
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape ball = new Ellipse2D.Float(i++, i++, 5, 5);
g2.setColor(Color.RED);
g2.fill(ball);
}
}
ScheduledExecutorService: schedule(Runnable command, long delay, TimeUnit unit)
/*
License for Java 1.5 "Tiger": A Developer"s Notebook
(O"Reilly) example package
Java 1.5 "Tiger": A Developer"s Notebook (O"Reilly)
by Brett McLaughlin and David Flanagan.
ISBN: 0-596-00738-8
You can use the examples and the source code any way you want, but
please include a reference to where it comes from if you use it in
your own products or services. Also note that this software is
provided by the author "as is", with no expressed or implied warranties.
In no event shall the author be liable for any direct or indirect
damages arising in any way out of the use of this software.
*/
import static java.util.concurrent.TimeUnit.SECONDS;
import java.io.PrintStream;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
public class Main {
public static void main(String[] args) {
// Get the scheduler
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
// Get a handle, starting now, with a 10 second delay
final ScheduledFuture<?> timeHandle =
scheduler.scheduleAtFixedRate(new TimePrinter(System.out), 0, 10, SECONDS);
// Schedule the event, and run for 1 hour (60 * 60 seconds)
scheduler.schedule(new Runnable() {
public void run() {
timeHandle.cancel(false);
}
}, 60*60, SECONDS);
/**
* On some platforms, you"ll have to setup this infinite loop to see output
while (true) { }
*/
}
}
class TimePrinter implements Runnable {
private PrintStream out;
public TimePrinter(PrintStream out) {
this.out = out;
}
public void run() {
out.printf("Current time: %tr%n", new Date());
}
}
ScheduledThreadPoolExecutor: scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Main {
public static void main(String[] a){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 300);
PaintSurface canvas = new PaintSurface();
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
executor.scheduleAtFixedRate(canvas, 0L, 100L, TimeUnit.MILLISECONDS);
f.add(canvas);
f.setVisible(true);
}
}
class PaintSurface extends JComponent implements Runnable{
int i = 0;
public void run() {
repaint();
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape ball = new Ellipse2D.Float(i++, i++, 5, 5);
g2.setColor(Color.RED);
g2.fill(ball);
}
}