2

I have created a thread on and called that on button click:

btnUpload.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         new Thread(new ClientThread()).start();
    }
});

This is my main thread:

class ClientThread implements Runnable {

        @Override
        public void run() {

            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

                socket = new Socket(serverAddr, SERVERPORT);

            } catch (UnknownHostException e1) {
                e1.printStackTrace();

            } catch (IOException e1) {
                e1.printStackTrace();

            }

        }

    }

I need to stop the thread after actions of button click. How will I do it? I have tried Boolean, interrupt methods but not working. How can I stop it? Please help me.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
androidGenX
  • 1,108
  • 3
  • 18
  • 44

4 Answers4

0

Try this test code:

public class Test3 {
    public static void main(String[] args) throws InterruptedException, IOException {
        Thread serverThread = new Thread(new ServerThread());
        serverThread.start();

        ClientThread client = new ClientThread();
        Thread clientThread = new Thread(client);
        clientThread.start();
        Thread.sleep(5000);
        client.closeSocket();
    }
}

class ServerThread implements Runnable {
    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(7777);
            serverSocket.accept();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

class ClientThread implements Runnable {
    Socket socket;

    public void closeSocket() throws IOException {
        socket.close();
    }

    @Override
    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName("127.0.0.1");
            socket = new Socket(serverAddr, 7777);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This succesfully closes socket and exits thread.

DmitryKanunnikoff
  • 2,226
  • 2
  • 22
  • 35
0

I guess what you are trying to do is to start the socket and then wait for some event (eg. a button click) and then stop it. You can use guarded blocks to wait on a flag.

Your class will look like this

class ClientThread implements Runnable {

    private static final int SERVERPORT = 0;
    private static final String SERVER_IP = null;
    Socket socket;
    boolean stopFlag;

    public ClientThread(){
        stopFlag = false;
    }

    public void setStopFlag(){
        stopFlag = true;
    }

    @Override
    public void run() {

        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            socket = new Socket(serverAddr, SERVERPORT);
            synchronized(this){
                while(!stopFlag){
                    try {
                        wait();
                    } catch (InterruptedException e) {}
                }
            }
            socket.close();

        } catch (UnknownHostException e1) {
            e1.printStackTrace();

        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

}

When you start the clientThread, hold the instance so that you can signal it later.

ClientThread ct = new ClientThread();
new Thread(ct).start();

When you want to stop the socket(eg. from a button click), do this

synchronized(ct){
   ct.setStopFlag();
   ct.notifyAll();
}

More information about guarded blocks can be found here - http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

I recommend you read the whole java concurrency tutorial -http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html

Concurrency is a very interesting and challenging topic. The syntax to start a thread may be simple enough but don't let that mislead you.

hummingV
  • 1,014
  • 1
  • 11
  • 25
-1

Call wait() method on the thread whenever button is clicked or event happens (whenever you desire) and again call notify() method to resume the thread

Stunner
  • 961
  • 2
  • 19
  • 39
  • Can you explain? These methods are using in the case of multiple threads! – androidGenX Mar 19 '14 at 09:26
  • I suggest you to read API of [Thread](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html) class methods first before you use them. – Stunner Mar 19 '14 at 09:29
  • multiple threads?? Yes. and for your information main method is also a thread. so when you create another thread, don't you have multiple threads??? Understand threads concept first and then use them. – Stunner Mar 19 '14 at 09:33
-1
private Thread thread = new Thread(new ClientThread());
private boolean isStarted = false;
btnUpload.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!isStarted)
                thread.start();
            else
                thread.stop();
    }
});

Try this.

Tony Lin
  • 765
  • 3
  • 15
  • 35