0

I implemented this class in my android code

I made the below change in the run method(replaced "true"):

@Override
    public void run() {
        while (!isInterrupted()) {
            try {
                // A blocking operation. Initiate a ChatManager instance when
                // there is a new connection
                pool.execute(new ChatManager(socket.accept(), handler));
                Log.d(TAG, "Launching the I/O handler");
            } catch (IOException e) {
                try {
                    if (socket != null && !socket.isClosed())
                        socket.close();
                } catch (IOException ioe) {
                }
                e.printStackTrace();
                pool.shutdownNow();
                break;
            }
        }
    }

I want to stop this thread before I close the app. So I implemented threadName.interrupt(); method. But this doesn't interrupt the thread.

I am actually confused with the usage of thread pool executor. So I am not sure how to do this efficiently. How can I implement interrupting this thread? When interrupt method is called, I want to close the socket, shutdown the pool and stop the thread.

NewOne
  • 401
  • 5
  • 19

3 Answers3

0
Thread thread = new Thread () {
boolean isRunning = true;

public void stopThread() {
    isRunning = false;
}

@Override
public void run() {
    while (isRunning) {
        try {
            // A blocking operation. Initiate a ChatManager instance when
            // there is a new connection
            pool.execute(new ChatManager(socket.accept(), handler));
            Log.d(TAG, "Launching the I/O handler");
        } catch (IOException e) {
            try {
                if (socket != null && !socket.isClosed())
                    socket.close();
            } catch (IOException ioe) {
            }
            e.printStackTrace();
            pool.shutdownNow();
            break;
          }
        }
    }
};
thread.start();

Try this code. and call thread.stopThread() whenever you want the thread to stop.

Akash Raghav
  • 1,073
  • 9
  • 19
0

if you want close an Android thread, you can set a variable to control run(),because run() is end, the thread will be closed.

The code is something like:

final boolean istrue=true;
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (istrue){
                //TODO  your work
            }

        }
    }).start();
}

If you want to close the thread, you only set istrue=false

lansus
  • 1
  • 1
0

Just call shutDownNow to close the pool and try interrupt all the threads inside it. You can check the difference in this post:

  • shutdown() will just tell the executor service that it can't accept new tasks, but the already submitted tasks continue to run
  • shutdownNow() will do the same AND will try to cancel the already submitted tasks by interrupting the relevant threads. Note that if your tasks ignore the interruption, shutdownNow will behave exactly the same way as shutdown.

If you want to interrupt or cancel an specific thread. I suggest you to use submit with Callables, With this, you will me able to work with your Future object, then if want to cancel a task you've given an executor service, you can call cancel(true) on its associated Future. When your task detects an interrupt request, it should preserve the interrupted status by calling Thread.currentThread().interrupt().

Community
  • 1
  • 1
Francisco Durdin Garcia
  • 12,540
  • 9
  • 53
  • 95