如何结束一个线程的运行

小水晶 2009-12-15 04:07:15
我的程序中需要对数据库执行一个时间很长的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,时间很长。
...全文
191 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
q1224006161 2011-07-27
  • 打赏
  • 举报
回复
public class TestInterrupt {


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;
}
}
}
}

huang_w 2009-12-16
  • 打赏
  • 举报
回复
顶! 楼主解决了 也给我们分享一下方法啊!
小水晶 2009-12-16
  • 打赏
  • 举报
回复
个人认为3楼,12楼,两位的见解比较有用,其他的各位还是先看清问题再回答吧
weihthchk 2009-12-15
  • 打赏
  • 举报
回复
10楼的,Thread的wait()是来自Object的,就是说所有类都有这个方法。这个方法不是让这个线程等待,而是调用wait()方法的那个线程等待,而且等待的是别的线程对同一个对象的notify或者notifiAll的调用,而不是一个指定的时长(指定那个时长是给出你想要最大等待的时间)。
ycscsjj 2009-12-15
  • 打赏
  • 举报
回复
zenzenm
michaellufhl 2009-12-15
  • 打赏
  • 举报
回复
see:java.sql.Statement.setQueryTimeout(int)

"Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds. If the limit is exceeded, an SQLException is thrown."
shadow55 2009-12-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 coldanimal 的回复:]
thread.destroy();
[/Quote]顶
princess_rosie 2009-12-15
  • 打赏
  • 举报
回复
thread.sleep(0);让线程睡觉
thread.exit(); 让线程终止;
thread.wait(); 让线程等待
hbu_zhy 2009-12-15
  • 打赏
  • 举报
回复
请参考6L。
Java&Oracle学习交流群,知无不言,言无不尽。欢迎大家交流分享学习工作心得。欢迎加入java技术交流学习QQ群:20378027。谢谢!
一头头 2009-12-15
  • 打赏
  • 举报
回复

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
小水晶 2009-12-15
  • 打赏
  • 举报
回复
楼上的,你那个太经典了,你的可以sleep,和执行一个长时间的SQL不一样。执行SQL会阻塞,不会把时间片交出来让你sleep的
一头头 2009-12-15
  • 打赏
  • 举报
回复
I know it is really a bad idea to use destroy or stop function.

Try the following code.

# public class AlternateStop extends Object implements Runnable {
# 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();
# }
# }
小水晶 2009-12-15
  • 打赏
  • 举报
回复
weihthchk的方法好像有意思,试试
小水晶 2009-12-15
  • 打赏
  • 举报
回复
1楼的,执行SQL时,线程自己也没有时间片呀,阻塞在哪了,thread.sleep不会被执行的

2楼哥们,destroy没实现吧
weihthchk 2009-12-15
  • 打赏
  • 举报
回复
如果不使用stop()来强行停止的话,就需要在数据库连接上动手脚了。
可以为被监控线程执行的代码设置一个状态(设为“执行中断”),在监控线程里面中止SQL执行(用Statement的close()),在被监控线程里面捕捉到SQLException后检查那个状态,如果是“执行中断”,停止就可以了。
一头头 2009-12-15
  • 打赏
  • 举报
回复
thread.destroy();
zabaglione 2009-12-15
  • 打赏
  • 举报
回复
thread.sleep(0)把线程控制权交出来,然后在return出去

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧