[1112] in java-interest
Setting up a signal handler
daemon@ATHENA.MIT.EDU (JD Brennan)
Fri Aug 18 23:17:54 1995
Date: Fri, 18 Aug 1995 17:16:27 -0700
From: JD Brennan <brennan@symbologic.com>
To: java-interest@java.sun.com
In-Reply-To: <133693896@toto.iv>
Reply-To: JD Brennan <brennan@symbologic.com>
Louis Theran writes:
> I am trying to set up a mechanism that will work in the same way that
> the following works in C:
>
> void
> wake_me(void(*)() func,int duration) {
> signal(SIGALRM,func);
> alarm(duration);
> }
Here's a simple one shot timer, and an example applet that uses it.
------timer.java------
import java.lang.Thread;
class timer implements Runnable {
private Thread kicker;
private Runnable target;
private int delay;
public timer(Runnable target, int milliseconds) {
this.target = target;
delay = milliseconds;
start();
}
public void start() {
kicker = new Thread(this);
kicker.start();
}
public void run() {
Thread.sleep(delay);
target.run();
kicker = null; // drop my ref so thread will get deleted
}
public void stop() {
if (kicker != null) {
kicker.stop();
kicker = null;
}
}
public void restart() {
stop();
start();
}
public boolean isRunning() {
return (kicker != null);
}
} // Timer
------counter.java------
import browser.Applet;
import awt.*;
import timer;
class counter extends Applet implements Runnable {
private final int DELAY = 250;
private int theCount = 0;
private timer kicker = null;
public void init() {
resize(450, 50);
kicker = new timer(this, DELAY);
}
public void paint(Graphics g) {
g.drawString("Counter is: " + String.valueOf(theCount), 50, 25);
}
public void run() {
theCount++;
repaint();
kicker = new timer(this, DELAY);
}
public void mouseUp(int x, int y) {
if (kicker == null) {
kicker = new timer(this, DELAY);
} else {
kicker.stop();
kicker = null;
}
}
} // counter
-----------------
JD Brennan
[brennan@symbologic.com Symbologic Corp., Seattle, WA]
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com