点击按钮后停止线程

时间:2014-03-19 09:22:45

标签: java android

我创建了一个线程并在按钮点击时调用它:

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

这是我的主线:

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();

            }

        }

    }

我需要在点击按钮后停止线程。我该怎么办?我尝试过布尔,中断方法,但没有工作。我怎么能阻止它?请帮帮我。

4 个答案:

答案 0 :(得分:0)

试试这个测试代码:

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

这会成功关闭套接字并退出线程。

答案 1 :(得分:0)

我想你要做的是启动套接字然后等待一些事件(例如点击按钮)然后停止它。您可以使用受保护的块等待旗帜。

您的课程将如下所示

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

}

当你启动clientThread时,按住实例以便稍后发出信号。

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

如果要停止套接字(例如,单击按钮),请执行此操作

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

有关防护区块的更多信息,请点击此处 - http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

我建议你阅读整个java并发教程 - http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html

并发是一个非常有趣且具有挑战性的话题。启动线程的语法可能很简单,但不要误解你。

答案 2 :(得分:-1)

在点击按钮或事件发生时(无论何时何时)调用线程上的wait()方法,并再次调用notify()方法恢复线程

答案 3 :(得分:-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();
    }
});

试试这个。