我的程序中需要对数据库执行一个时间很长的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,时间很长。

解决方案 »

  1.   

    thread.sleep(0)把线程控制权交出来,然后在return出去
      

  2.   

    如果不使用stop()来强行停止的话,就需要在数据库连接上动手脚了。
    可以为被监控线程执行的代码设置一个状态(设为“执行中断”),在监控线程里面中止SQL执行(用Statement的close()),在被监控线程里面捕捉到SQLException后检查那个状态,如果是“执行中断”,停止就可以了。
      

  3.   

    1楼的,执行SQL时,线程自己也没有时间片呀,阻塞在哪了,thread.sleep不会被执行的2楼哥们,destroy没实现吧
      

  4.   

    weihthchk的方法好像有意思,试试
      

  5.   

    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();
    #     }
    # }
      

  6.   

    楼上的,你那个太经典了,你的可以sleep,和执行一个长时间的SQL不一样。执行SQL会阻塞,不会把时间片交出来让你sleep的
      

  7.   


    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
      

  8.   

    请参考6L。
    Java&Oracle学习交流群,知无不言,言无不尽。欢迎大家交流分享学习工作心得。欢迎加入java技术交流学习QQ群:20378027。谢谢!
      

  9.   

    thread.sleep(0);让线程睡觉
    thread.exit();  让线程终止;
    thread.wait(); 让线程等待
      

  10.   

    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."
     
      

  11.   

    10楼的,Thread的wait()是来自Object的,就是说所有类都有这个方法。这个方法不是让这个线程等待,而是调用wait()方法的那个线程等待,而且等待的是别的线程对同一个对象的notify或者notifiAll的调用,而不是一个指定的时长(指定那个时长是给出你想要最大等待的时间)。
      

  12.   

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