我的程序中需要对数据库执行一个时间很长的SQL,该SQL是用户提供的,因此不同的SQL执行时间可能不同,有缺乏经验的用户就可能提供一个SQL执行1个来小时,因此我的程序中t = new Thread()来执行这个SQL,然后
synchronized(t){
t.wait(30*60*1000);
}
等30分钟,要执行完,run()里有notify;要是没执行完,wait超时自己醒了,我要关t这个线程。
t.stop()有效果,但不推荐了。
有什么方法能实现。run函数里没有循环就是执行那个SQL,时间很长。
synchronized(t){
t.wait(30*60*1000);
}
等30分钟,要执行完,run()里有notify;要是没执行完,wait超时自己醒了,我要关t这个线程。
t.stop()有效果,但不推荐了。
有什么方法能实现。run函数里没有循环就是执行那个SQL,时间很长。
可以为被监控线程执行的代码设置一个状态(设为“执行中断”),在监控线程里面中止SQL执行(用Statement的close()),在被监控线程里面捕捉到SQLException后检查那个状态,如果是“执行中断”,停止就可以了。
# private volatile boolean stopRequested;
# private Thread runThread;
#
# public void run() {
# runThread = Thread.currentThread();
# stopRequested = false;
#
# int count = 0;
#
# while ( !stopRequested ) {
# System.out.println("Running ... count=" + count);
# count++;
#
# try {
# Thread.sleep(300);
# } catch ( InterruptedException x ) {
# Thread.currentThread().interrupt(); // re-assert interrupt
# }
# }
#
# System.out.println("stoped");
# }
#
# public void stopRequest() {
# stopRequested = true;
#
# if ( runThread != null ) {
# runThread.interrupt();
# }
# }
#
# public static void main(String[] args) {
# AlternateStop as = new AlternateStop();
# Thread t = new Thread(as);
# t.start();
#
# try {
# Thread.sleep(2000);
# } catch ( InterruptedException x ) {
# // ignore
# }
#
# as.stopRequest();
# }
# }
import java.io.BufferedReader;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.Channels;public class InterruptInput {
static BufferedReader in = new BufferedReader(
new InputStreamReader(
Channels.newInputStream(
(new FileInputStream(FileDescriptor.in)).getChannel())));
public static void main(String args[]) {
try {
System.out.println("Enter lines of input (user ctrl+Z Enter to terminate):");
System.out.println("(Input thread will be interrupted in 10 sec.)");
// interrupt input in 10 sec
(new TimeOut()).start();
String line = null;
while ((line = in.readLine()) != null) {
System.out.println("Read line:'"+line+"'");
}
} catch (Exception ex) {
System.out.println(ex.toString()); // printStackTrace();
}
}
public static class TimeOut extends Thread {
int sleepTime = 10000;
Thread threadToInterrupt = null;
public TimeOut() {
// interrupt thread that creates this TimeOut.
threadToInterrupt = Thread.currentThread();
setDaemon(true);
}
public void run() {
try {
sleep(10000); // wait 10 sec
} catch(InterruptedException ex) {/*ignore*/}
threadToInterrupt.interrupt();
}
}
}看这个http://forward.com.au/javaProgramming/HowToStopAThread.html
Java&Oracle学习交流群,知无不言,言无不尽。欢迎大家交流分享学习工作心得。欢迎加入java技术交流学习QQ群:20378027。谢谢!
thread.exit(); 让线程终止;
thread.wait(); 让线程等待
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mt.flag = false; }}class MyThread extends Thread{
boolean flag = true;
public void run(){
while(flag){
System.out.println("***"+new Date()+"***");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
}
}
}