我正在尝试使这个简单的示例起作用:
import java.io.*;
public class Timer {
public static void main(String[] args) {
setTimer(new Runnable() {
public void run() {
System.out.println("tick");
}
},
1000 /* msecs */
);
while (true) ;
}
native public static void setTimer(Runnable r, int msecs);
static {
System.loadLibrary("timer");
}
}
代码很简单。在JNI setTimer
/C/Linux 中使用SIGALRM
和setitimer
. 示例应该每秒打印一次刻度。
JNI 库被编程为:
#include <signal.h>
#include <sys/time.h>
#include "Timer.h"
#include "error.h"
static JNIEnv *genv;
static jobject gobj;
void handler(int s) {
jclass cls = (*genv)->GetObjectClass(genv, gobj);
jmethodID mid = (*genv)->GetMethodID(genv, cls, "run", "()V");
(*genv)->CallVoidMethod(genv, gobj, mid);
}
JNIEXPORT void JNICALL Java_Timer_setTimer
(JNIEnv *env, jclass cls, jobject obj, jint msecs) {
struct sigaction sa;
struct itimerval it;
sa.sa_flags = SA_RESTART;
sigemptyset (&sa.sa_mask);
genv = env;
gobj = obj;
sa.sa_handler = handler;
it.it_interval.tv_sec = msecs/1000;
it.it_interval.tv_usec = (msecs%1000)*1000;
it.it_value = it.it_interval;
if (sigaction (SIGALRM/*PROF*/, &sa, NULL) == -1
|| setitimer (ITIMER_REAL/*PROF*/, &it, NULL) == -1)
SysError("setTimer: sigaction, setitimer: couldn't init timer");
}
我正在使用信号链库,以免对 JVM 产生信号干扰:
LD_LIBRARY_PATH=. LD_PRELOAD=/usr/lib/jvm/java-6-openjdk/jre/lib/i386/libjsig.so java Timer
SIGALRM 第一次使用消息触发 JMV 段错误:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00e9531c, pid=5742, tid=3077924544
#
...
为什么?