我開發了一個旨在允許用戶執行查詢的應用程序。一旦用戶輸入查詢並單擊執行按鈕,控制權就被傳遞給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()完成。 我想在運行時停止執行
你如何阻止線程?爲什麼upload()方法在中間調用stop()? –
我想檢查一下在兩者之間運行時是否可以停止線程,只是爲了檢查我的停止塊是否正常工作 – happy
您知道線程在完成upload()之前不會響應任何「stop()」請求, '方法,對吧?您必須輪詢'upload()'方法內的'finished'標誌以終止此處。 – erickson