2

I'm trying to get familiar with RMI, after creating my server and client. I'm looking now to create a method that will allow an AdministratorClient to stop a thread (in case it takes too long to process).

I tried using

interrupt()

but when I get the stackTrace of all thread I still see the one that was supposed to be stopped.

Here's the code for the AdminMethod (in the server), where I'm trying to implement the killThread method :

 import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.Set;

    public class AdminMethod extends UnicastRemoteObject implements AdminInterface{
        protected AdminMethod() throws RemoteException {}

        public  String getThreadList() throws RemoteException {
            Set<Thread> setOfThread =Thread.getAllStackTraces().keySet();

            return setOfThread.toString();
        }


        public void killThread(long ThreadId) throws RemoteException {
            Set<Thread> setOfThread = Thread.getAllStackTraces().keySet();


            for(Thread thread : setOfThread){
                if(thread.getId()==ThreadId){
                    thread.interrupt();
                }
            }

        }



    }

And here's the AdministratorClient :

import java.rmi.Naming;
import java.util.Scanner;


public class AdministrationClient {
    public static void main (String[] args) {
        AdminInterface AdminProxy;
        try {

            AdminProxy = (AdminInterface)Naming.lookup("rmi://localhost/CBA");
            String ThreadList = AdminProxy.getThreadList();
            System.out.println(""+ThreadList);
            Scanner sc = new Scanner(System.in);
            System.out.println("Veuillez saisir un ID :");
            long Id = sc.nextLong();
            AdminProxy.killThread(Id);
            System.out.println("Le thread "+Id+" a été correctement supprimé");
            ThreadList = AdminProxy.getThreadList();
            System.out.println(""+ThreadList);
            sc.close();
            }catch (Exception e) {
                System.out.println("AdminClient exception: " + e);
                }

        }
}

I've seen numerous answers about a way to stop a thread, with using a flag. But I don't know how to implement it since the thread is created automatically, it's not a custom thread of which I can override the run() method.

Can you please tell me what can I do about this ? Even a hint would do ! Thanks

  • It depends on the logic of the thread that you are interrupting. If it ignores interruption, I guess what you can do is simply respecting its decision – Adrian Shum Sep 23 '16 at 03:02
  • Read `Thread`'s doc plz , `interrupt()` just set a flag to the thread , not to stop it . – passion Sep 23 '16 at 03:12
  • I know that the interruption isn't working ! That's why I came here for help, I can't just "respect" it's decision to not stop because the exercice itself is about stopping the thread. Thanks for your very helpful answers ! – Hicham Benhima Sep 23 '16 at 03:38
  • Any Java exercise that's about stopping threads is fundamentally misconceived. You can only reliably stop a Java thread if you can get it to check `Thread.interrupted()` and friends, and exit, or exit if it catches `InterruptedException`, both of which basically imply that you wrote it. RMI has nothing to do with that. – user207421 Sep 23 '16 at 03:50
  • The above comments are the way a thread needs to be stopped. However (purely from academic angle) you could use Thread.stop() [this method is depricated and please refer to the javadoc for details as to why] and see if that actually stops the thread and has the intended behaviour[Thread.stop() should not be used in production situation] – Ironluca Sep 23 '16 at 07:03
  • The stop() method can't be used at all, it is set to always return a ThreadDeath exception. I'll try to do it from a different angle. Thank you though – Hicham Benhima Sep 25 '16 at 01:05

0 Answers0