2

I'm using the following code in my onCreate method:

@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();
            }
        });
    }

It gives me a NetworkOnMainThreadException!

It does work when I use the following lines to remove the restriction:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy); 

But why should I use it?

I used thread, why isn't it running on the separated thread and runs on the UI thread?!

MTP1376
  • 67
  • 9

3 Answers3

8

Change

thread.run();

to

thread.start();

Difference:

Thread.run() does not spawn a new thread whereas Thread.start() does, i.e Thread.run actually runs on the same thread as that of the caller whereas Thread.start() creates a new thread on which the task is run.

For more info see Difference between running and starting a thread

Community
  • 1
  • 1
Giru Bhai
  • 14,097
  • 5
  • 43
  • 70
2

Because you are not asking the thread to start.

Replace:

thread.run();

With

thread.start();

Calling run() will simply execute the provided Runnable on the current thread. However, start() will spawn the execution onto a new thread.

waqaslam
  • 64,866
  • 15
  • 157
  • 170
  • Hahaahaaaaaaaaaa :))) Tnx :))) – MTP1376 Aug 15 '14 at 09:42
  • @MTP1376 you should have selected this guy's answer because it does a better job of explaining what goes on: .start() is the method that the Java library provides to start a thread. .run() is the (tragically named) method that _you_ provide that tells the thread what to do. – Solomon Slow Aug 15 '14 at 13:45
2

Change from thread.run() to thread.start()


  @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.start();
                }
            });
        }
Devrath
  • 37,389
  • 47
  • 165
  • 245