线程不间断

时间:2019-06-06 14:24:56

标签: android multithreading handler

我正在使用板条箱服务器和android客户端 但是线程不会被android客户端打断 我的Android客户端从服务器请求了RoomList 和服务器接收,客户端接受 并使用适配器填写我的ListView 如果点击后退按钮,这是一个问题, 比RoomListAcitivity关闭并且线程已停止 但是线程并没有在我的应用程序中存活下来 首先Enter已成功进行 但按下BackButton并重新输入此活动 MyApp只是白色,没有动作 我如何才能停止该线程? (并对我的英语水平感到抱歉...)

我尝试了.interrupt()方法和handler.removeMessages(0) 但是失败的线程仍然存在

上传完整的Java代码,以防万一...

   ListView roomList;
    RoomAdapter roomAdapter;
    Socketservice ss;
    String msg,rtitle;
    String msgs[];
    String list[];
    Thread listthread,EnterRoomThread,removeV;
    boolean staterun = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_room);

        roomList = findViewById(R.id.roomList);
        roomAdapter = new RoomAdapter();

        listthread = new Thread() {
            public void run(){
                        ss.out.println("163|");
                        ss.out.println("100|");
                try {
                    while (staterun == true) {
                            msg = ss.in.readLine();
                            handler.post(new Runnable() {
                                public void run() {
                                    msgs = msg.split("\\|");
                                    String protocol = msgs[0];
                                    switch (protocol) {
                                        case "163":
                                            list = msgs[1].split(",");
                                            for (int i = 0; i < list.length; i++) {
                                                String list1[] = list[i].split("-");
                                                String listT = list1[0];
                                                int listC = Integer.parseInt(list1[1]);
                                                int listI = Integer.parseInt(list1[2]);
                                                roomAdapter.CreateRoom(listI, listT, listC);
                                            }
                                                roomList.setAdapter(roomAdapter);
                                                msg = "";
                                                msgs = null;
                                                break;
                                        case "200":
                                            Intent i = new Intent(getApplicationContext(), GameWaitingActivity.class);
                                            i.putExtra("tname", rtitle);
                                            staterun = !staterun;
                                            Toast.makeText(getApplicationContext(),""+listthread.isAlive(),Toast.LENGTH_SHORT).show();
                                            startActivity(i);
                                            finish();
                                            break;
                                        case "201":
                                            Toast.makeText(getApplicationContext(), "방이 꽉 찼습니다.", Toast.LENGTH_SHORT).show();
                                            break;
                                    }
                                }
                            });
                    }
                } catch (IOException e) {

                }
            }
        };
        listthread.start();

        roomList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Room item = (Room) roomList.getItemAtPosition(position);
                rtitle=item.getTitle();
                EnterRoomThread = new Thread() {
                    public void run() {
                        ss.out.println("200|" + rtitle);
                        EnterRoomThread.interrupt();
                    }
                };
                EnterRoomThread.start();
        }


    });

    }

    Handler handler = new Handler();




    @Override
    public void onBackPressed() {
        removeV = new Thread() {
            public void run(){
                ss.out.println("101|");
                removeV.interrupt();
            }
        };
        removeV.start();
        handler.removeMessages(0);
        staterun = false;
        listthread.interrupt();
        Toast.makeText(getApplicationContext(),""+listthread.isAlive(),Toast.LENGTH_SHORT).show();
        finish();
    }
}

1 个答案:

答案 0 :(得分:0)

继续进行,在run()方法中编写

    //use this boolean value to keep track.
    boolean shouldRunFlag = true;
    while (shouldRunFlag) {
            try {
                Thread.sleep(10);  

                //Do your work............

            } catch (Exception e) {
                e.printStackTrace();
                Log.v(TAG, "Interrupted Exception caught");

                // change the flag, so that while loop can be broken.

                shouldRunFlag = false;
                Log.v(TAG, "run: breaking the loop");
                break;
            }

    }

这就是您中断和清洁线程的方式

 private void interrupt(Thread currentThread) {
        Log.i(TAG, "interrupt");
        if (currentThread != null) {
            Thread dummyThread = currentThread;
            dummyThread.interrupt();
            currentThread = null;
        }
    }