0

I have developed an application which is intended to allow users to execute queries.Once the user enters the query and clicks on execute button the control is passed to RMI server which in turn starts the thread.

User should be able to execute other quesries one after the other and each query will b executed in different thread.

I am not able to stop the execution of the thread. I want to stop the execution while it is executing or on the button click event based on the thread id passed. I am trying below code

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

RMI server class from where I start the thread

        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 client

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

Till my knowlegde p.killJob() will wont be invoked untill executeJob() is finished. I want to stop the execution while it is running

happy
  • 2,550
  • 17
  • 64
  • 109
  • And how do you stop the thread? Why does the upload() method call stop() in the middle? – JB Nizet Jun 14 '12 at 05:53
  • I want to check that while running in between I can stop the thread,just to check if my stop block is working – happy Jun 14 '12 at 05:55
  • You know that the thread will not respond to any `stop()` request until it completes the `upload()` method, right? You'd have to poll the `finished` flag inside the `upload()` method to abort there. – erickson Jun 14 '12 at 06:20
  • yeah,thats why I have added stop() in upload method which in turn has finished flag set to true. – happy Jun 14 '12 at 06:22
  • You are creating many different `AcQueryExecutor` instances, but you don't call `a.stop()` on any of them. What `stop()` message do you think is ignored? – erickson Jun 14 '12 at 06:24
  • I have edited the code,added a.stop() in RMI server.But in my RMI client p.killJob() will wont be invoked untill executeJob() is finished.I want to stop the execution while it is running to check if I can stop the execution – happy Jun 14 '12 at 06:42

1 Answers1

0

The problem appears to be that you are allocating a fresh instance of the Runnable AcQueryExecutor for each thread. This means each is seeing its own finished flag. Setting one in killJob will not lead to any other thread exiting because no other thread shares this flag.

You'll need to share Runnables or else make the finish field static. The latter will cause all threads to exit whenever any instance's stop is called, so may not be what you want.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • How can I do this without creating frsh instance of AcQueryExecutor?where should I create the instance and share it? – happy Jun 14 '12 at 06:56
  • Create the AcQueryExecutor in executeJob. Then find a way to make it visible to killJob. Either you can save it in the Server class (but then your server has only have one job in progress at a time; I can't tell from the code if this is what's intended), or you can return it from executeJob to serve as a "handle" passed to killJob. In the latter case you can cast to/from `Object` to emphasize that it's just a handle. You could also keep in the Server a registry that maps string names to to AcQueryExecutors. – Gene Jun 14 '12 at 07:38
  • there should be multiple jobs in progress asynchronously.And I'l try what u suggested.thank you – happy Jun 14 '12 at 07:47