1

I spent many days searching a solution for my java program but without results. My program dynamically loads plugins(class with same interface) from jar package that are nothing more threads that make requests to devices with different protocols , some of these can be blocked without way to interrupt it, for example a plugin poorly designed or indefinitely blocked on I/O requests .I have read many posts on use of a flag in a loop; or closing socket in case of blocked I/O but in my situation I don't know what kind of communication plugins are using so I can not predict in advance what type of error may be related to the blocked thread . Is there a way to be able to stop thread without having to finish the VM or without having to wait for it to end blocked I/O ? This is what my program do (small snippet only an overview ):

LinkedList<FutureTask<ArrayList<MyObject>>> queue = new LinkedList<FutureTask<ArrayList<MyObject>>>();
FutureTask task = new FutureTask<ArrayList<MyObject>>(data);
/* Foreach task(plugin) insert it into queue*/
/*Sumbit each task in a loop */
executor.submit(queue.get(k));
/*Wait for a specific timeout*/
queue.get(i).get(mTimeout, TimeUnit.MILLISECONDS);
/*catch all type of exception to log errors*/
catch (Exception e) 
/*at the end force shutdown BUT IT DOESN'T WORK FOR A BLOCKED THREAD*/
executor.shutdownNow();

and plugin must extends this class for talk with my app:

package com.pack.adapter;
import java.util.ArrayList;
import java.util.concurrent.Callable;

import com.pack.base.MyObject;

public abstract class PluginImpl extends Thread implements Callable<ArrayList<MyObject>> {
    protected ArrayList<MyObject> buffer;

    public PluginImpl(){
        super();
    }

    @Override
    public void interrupt() {
        super.interrupt();
    }

    @Override
    public ArrayList<MyObject> call() {
        try {  
            return OnParamsRequest( buffer); //I DON'T KNOW IF THIS FUNC. CAN BLOCK PLUGIN'S THREAD IT'S ABSTRACT FUNCTION
        } catch (Exception e) {
            if (!isInterrupted()) {
                e.printStackTrace();
            } else {
                Thread.currentThread().interrupt();
                System.out.println("Interrupted");
            }
        }
        System.out.println("Shutting down thread "+getPluginName());
        return buffer;
    }


    @Override
    public void run() {
        if(getName() != null && getName() != ""){
            Thread.currentThread().setName(getName());
        }
        super.run();
    }

    abstract public void setValues(ArrayList<MyObject> buffer);

    abstract public ArrayList<MyObject> OnParamsRequest(ArrayList<MyObject> buffer) throws Exception; //this func. can block for a long time without way to stop this thread...there is way to fix it?

    abstract public String getPluginName();
}

There is a way to stop it? Or there is a way to destroy it and avoid different behaviors? Thank you

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
gianmarco
  • 11
  • 3

0 Answers0