小编MTP*_*376的帖子

为什么我的代码在主线程上运行?

我在onCreate方法中使用以下代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View arg0) {
                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            ServerSocket serverSocket = new ServerSocket(9002);
                            Socket s = serverSocket.accept();

                            DataOutputStream outputStream = new DataOutputStream(
                                    s.getOutputStream());
                            BufferedReader inputStream = new BufferedReader(
                                    new InputStreamReader(s.getInputStream()));

                            outputStream.write("Howdy! newbie".getBytes());

                            s.close();

                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });
                thread.run();
            }
        });
    } …
Run Code Online (Sandbox Code Playgroud)

java multithreading android

2
推荐指数
1
解决办法
115
查看次数

标签 统计

android ×1

java ×1

multithreading ×1