Monday, February 17, 2014

android thread


Since the handler and the activity is run in the same thread, running in Thread () method Thread.sleep (5000), Why Thread the System.out.println without first implemented, but the first implementation of Activity in System.out . println ();
 package cc.handler2; 

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

private Handler handler = new Handler ();
@ Override
protected void onCreate (Bundle savedInstanceState) {

super.onCreate (savedInstanceState);
handler.post (thread);
/ / Thread.start ();
setContentView (R.layout.activity_main);
System.out.println (. "Thread333 -" + Thread.currentThread () getId ());
System.out.println (. "Thread333 -" + Thread.currentThread () getName ());

}

Thread thread = new Thread ()
{

public void run ()
{
System.out.println ("thread" + Thread.currentThread () getId ().);
System.out.println ("thread" + Thread.currentThread () getName ().);
try {
Thread.sleep (5000);
} Catch (InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
System.out.println ("thread" + Thread.currentThread () getId ().);
System.out.println ("thread" + Thread.currentThread () getName ().);


};
};
@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.main, menu);.
return true;
}

}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
The same question, I hope an expert answer

No comments:

Post a Comment