我在android中创建了一个用于多线程的程序。
当我点击其中一个按钮时,它的线程启动并将值打印到 EditText 现在我想确定该线程是否正在运行,以便我可以在单击时停止线程(如果它正在运行)并启动一个新线程(如果它没有在此处运行)是亩代码:
public void startProgress(View view) {
        final String v;
        if(view == b1)
        {
            v = "b1";
        }
        else
        {
            v = "b2";
        }
        // Do something long
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //for (int i = 0; i <= 10; i++) {
                while(true){
                    if(v.equals("b1"))
                    {
                        i++;
                    }
                    else if(v.equals("b2"))
                    {
                        j++;
                    }
                    try {
                        if(v.equals("b1"))
                        {
                            Thread.sleep(3000);
                        }
                        else if(v.equals("b2"))
                        {
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //  progress.setProgress(value);
                            if(v.equals("b1"))
                            {
                                String strValue = ""+i;
                                t1.setText(strValue);
                            }
                            else
                            {
                                String strValue = ""+j;
                                t2.setText(strValue);
                            }
                            //t1.setText(value);
                        }
                    });
                }
            }
        };
        new Thread(runnable).start();
    }
@Override
    public void onClick(View v) {
        if(v == b1)
        {
            startProgress(b1);
        }
        else if(v == b2)
        {
            startProgress(b2);
        }
    }