我如何在java中停止一个Thread,我在停止点看到一条线?

时间:2015-11-02 18:38:40

标签: java android-studio

我正在使用Android Studio 1.3.2

我有这个线程,我在我的代码中的其他地方启动它,如下所示:

serverChecksThread.Start();

这是线程代码:

Thread serverChecksThread = new Thread(new Runnable()
{

    @Override
    public void run()
    {
        Looper.prepare();
        Looper l = Looper.myLooper();
        customHandler = new Handler(l);
        customHandler.postDelayed(serverChecksRunnable, 0);
        Looper.loop();    
    }
});

    Runnable serverChecksRunnable = new Runnable()
    {
        @Override
        public void run()
        {
            if (startuploadstatusthread == true))
            {
                checkServer = Get(iptouse + "uploadstatus");

            }

            Handler h=new Handler(Looper.getMainLooper());
            h.post(new Runnable()
            {
                @Override
                public void run()
                {
                    if (startuploadstatusthread == true))
                    {
                        if (checkServer != null)
                        {
                            String a = null;
                            try
                            {
                                a = new String(checkServer, "UTF-8");
                                textforthespeacch = a;
                                if (textforthespeacch.contains("upload completed"))
                                {
                                    String[] parts = textforthespeacch.split(",");
                                    String varr = parts[0];
                                    String varr1 = parts[1];
                                    String varr2 = parts[2];

                                    textforthespeacch = varr;
                                    status1.setText("Upload completed" + " " + varr1 + "%");
                                    timerValue.setText(varr2);
                                    numberofuploadedfilescounter += 1;
                                    uploadedfilescount.setText(("Uploaded Files: " + numberofuploadedfilescounter));
                                    startuploadstatusthread = false;
                                    MainActivity.this.initTTS();
                                    serverChecksThread.stop();
                                }
                                if (textforthespeacch.contains("uploading"))
                                {
                                    String[] split = textforthespeacch.split(" ");
                                    textforthespeacch = split[0];
                                    status1.setText("Uploading" + " " + split[1] + "%");
                                    servercheckCounter += 1;
                                    if (servercheckCounter == 1)
                                    {
                                        MainActivity.this.initTTS();
                                    }
                                }
                            } catch (UnsupportedEncodingException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            });

            customHandler.postDelayed(serverChecksRunnable,1000);
        }
    };

在Runnable里面我做了:

serverChecksThread.stop();

我没有测试它,但是一旦我写了serverChecksThread.stop();我在stop()上看到一条线:

Stop

停止的这条线是什么意思?

0 个答案:

没有答案