1

如何正确停止或中断我的多个服务器的线程?这是我的主程序的一部分,在这里我正在启动我的多个服务器,但我想实现停止按钮,导致我的程序在我中断它时总是卡住。我可以在这里做主要部分吗?我应该使用哪个功能?

button.setOnClickListener(new View.OnClickListener() {
        //OnClickListener oclbutton=new OnClickListener(){

            @Override
            public void onClick(View v) {

                tvOut.setText("Server Started");
                Thread thread=new Thread (new MyServer());
                thread.start();
            }
        });

        //button.setOnClickListener(oclbutton);
    //}

        buttonStop.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {

              //stop server


           }
         });
4

3 回答 3

1

First, you have to declare your thread variable outside onClick to be able to access it from the second listener:

final Thread thread=new Thread (new MyServer());

Please pay attention on final modifier: you are using this variable in anonymous inner classes that can access only final variables of outer method.

Now your first onClick() will just start the thread.

       public void onClick(View v) {
            tvOut.setText("Server Started");
            thread.start();
        }

Now, how to stop the thread?

You can call thread.stop(). But its method is deprecated for various reasons that beyond of this discussion. You can find reasons in internet.

So, how to stop the thread? It depends on what is it doing. You can either check isInterrupted() into thread and in this case call thread.interrupt() from your second listener. Other way is to use boolean flag and wait() - notify() pair.

There are a lot of tutorials about threads. Read some of them. If something is still unclear come here again and ask more concrete questions. Good luck and enjoy.

于 2013-04-25T18:02:33.067 回答
0

停止线程的一个好方法是使用标志正确退出 run() 方法:请参阅此答案

如果 myServer 正在等待 Thread.sleep() 您可以调用 myServer.interrupt() 来取消睡眠。
如果 myServer 在 I/O 上阻塞,那就有点复杂了:看看这个

于 2013-04-25T18:07:15.207 回答
0

如果您想从不同的块访问线程,您应该将其声明为所有方法或类都可以访问的字段或最终变量,而不是在内部匿名类上,像这样

public void yourMethod() {
    final Thread yourThread = null; 

    button.setOnClickListener(new View.OnClickListener() {
    //OnClickListener oclbutton=new OnClickListener(){

        @Override
        public void onClick(View v) {
            tvOut.setText("Server Started");
            yourThread=new Thread (new MyServer());
            thread.start();
        }
    });

    buttonStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //stop server
            yourThread.stop();
        }
    });
}

现在,请注意 Thread.stop() 方法已被弃用,因此您可以在 MyServer 类上执行以下操作,而不是进行该调用:

public class MyServer implements Runnable {

    private boolean stopExecution = false;

    public void setStopExecution(boolean stopExecution) {
        this.stopExecution = stopExecution;
    }

    public void run() {
        // as it is a server I assume you're doing a never ending loop
        // now just make use of the control variable
        while (!stopExecution) {
            // do your stuff here...
        }
    }
}

在你的代码上你要做的是:

public void yourMethod() { MyServer server = new MyServer();

    button.setOnClickListener(new View.OnClickListener() {
    //OnClickListener oclbutton=new OnClickListener(){

        @Override
        public void onClick(View v) {
            tvOut.setText("Server Started");
            Thread yourThread=new Thread (server);
            thread.start();
        }
    });

    buttonStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //stop server
            server.setStopExecution(true);
        }
    });
}

最后一种方法是避免显示器问题的正确方法。还可以查看 Executor 类来管理您的线程。

于 2013-04-25T18:03:12.083 回答