2012-06-14 145 views
0

我开发了一个旨在允许用户执行查询的应用程序。一旦用户输入查询并单击执行按钮,控制权就被传递给RMI服务器,RMI服务器又启动线程。无法停止执行

用户应该能够依次执行其他问题,并且每个查询将在不同的线程中执行。

我无法停止执行线程。我想要在执行时停止执行,或者在基于传递的线程ID的按钮单击事件时停止执行。 我想下面的代码

public class AcQueryExecutor implements Runnable { 
    private volatile boolean paused = false;  
    private volatile boolean finished = false; 
    String request_id="",usrnamee="",pswd="",driver="",url=""; 

    public AcQueryExecutor(String request_id,String usrnamee,String pswd,String driver,String url) { 
     this.request_id=request_id; 
     this.usrnamee=usrnamee; 
     this.pswd=pswd; 
     this.url=url; 
     this.driver=driver; 
    } 

    public void upload() throws InterruptedException { 
     //some code     
     stop(); 
     //some more code 
    } 

    public void run() { 
     try { 
      while(!finished) { 
       upload(); 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void stop() { 
     finished = true; 
    } 
} 
从我开始线程

 public class ExecutorServer extends UnicastRemoteObject implements ExecutorInterface 

     { 
     public ExecutorServer()throws RemoteException 
     { 
     System.out.println("Server is in listening mode"); 
     } 
     public void executeJob(String req_id,String usrname,String pwd,String driver,String url)throws RemoteException 
{ 
    try{ 
    System.out.println("Inside executeJob.wew.."); 
    AcQueryExecutor a=new AcQueryExecutor(req_id,usrname,pwd,driver,url); 
    Thread t1 = new Thread(a); 
    t1.start(); 
    } 
    catch(Exception e) 
    { 
     System.out.println("Exception " + e); 
    } 

}   
     public void killJob(String req_id)throws RemoteException{ 
      logger.debug("Kill task"); 
      AcQueryExecutor a=new AcQueryExecutor(req_id,"","","",""); 
    a.stop(); 
        } 


     public static void main(String arg[]) 
     { 
     try{ 
      LocateRegistry.createRegistry(2007); 
     ExecutorServer p=new ExecutorServer(); 
     Naming.rebind("//localhost:2007/exec1",p); 
     System.out.println ("Server is connected and ready for operation."); 
     }catch(Exception e) 
     { 
     System.out.println("Exception occurred : "+e.getMessage()); 
     e.printStackTrace(); 
     } 
     } 
       } 

RMI客户

ExecutorInterface p=(ExecutorInterface)Naming.lookup("//localhost:2007/exec1"); 
      System.out.println("Inside client.."+ p.toString()); 
        p.executeJob(id, usrname, pswd); 
       p.killJob(id); 
      } 

直到我knowlegde p.killJob()

RMI服务器类将不会被调用直到executeJob()完成。 我想在运行时停止执行

+0

你如何阻止线程?为什么upload()方法在中间调用stop()? –

+0

我想检查一下在两者之间运行时是否可以停止线程,只是为了检查我的停止块是否正常工作 – happy

+0

您知道线程在完成upload()之前不会响应任何“stop()”请求, '方法,对吧?您必须轮询'upload()'方法内的'finished'标志以终止此处。 – erickson

回答

0

问题似乎是您为每个线程分配RunnableAcQueryExecutor的新实例。这意味着每个人都看到自己的finished标志。在killJob中设置一个不会导致任何其他线程退出,因为没有其他线程共享此标志。

您需要分享Runnable s或使finish字段static。后者将导致所有线程在任何实例的stop被调用时退出,因此可能不是您想要的。

+0

如何在不创建AcQueryExecutor的frsh实例的情况下做到这一点?我应该在哪里创建实例并共享它? – happy

+0

在executeJob中创建AcQueryExecutor。然后找到一种方法让它可见killJob。要么你可以将它保存在Server类中(但是你的服务器一次只能有一个工作正在进行;我不能从代码中知道这是否是预期的),或者你可以从executeJob中返回它作为一个传递给killJob的“句柄”。在后一种情况下,您可以从Object中强制转换,以强调它只是一个句柄。您还可以在Server中保留一个将字符串名称映射到AcQueryExecutors的注册表。 – Gene

+0

应该有多个工作异步进行。我会尝试你的建议。谢谢你 – happy