我开发了一个旨在允许用户执行查询的应用程序。一旦用户输入查询并单击执行按钮,控制权就被传递给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