SoFunction
Updated on 2025-03-11

Two implementation methods of Android thread thread thread (must-read)

There are three points in this article that need to be explained in advance.

1: There are two ways to implement thread thread in android:

One is, the extension class

Another way is to implement the Runnable interface

Two: Thread class represents thread class, its two main methods are:

run() - contains the code executed when the thread is running

Start() - used to start a thread

Three: Handler mechanism, which is the bridge between Runnable and Activity interaction. Message is sent in the run method, and in the Handler, different tasks are performed through different Messages.

The following are two thread implementation methods. One is to extend the class, which means to write the run() method into the thread:

package ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
public class Demo_For_Copy extends Activity 
{ 
  public Button button; 
   
  public Handler mHandler=new Handler() 
  { 
    public void handleMessage(Message msg) 
    { 
      switch() 
      { 
      case 1: 
        (.text2); 
        break; 
      default: 
        break;    
      } 
      (msg); 
    } 
  }; 
   
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) 
  { 
    (savedInstanceState); 
    setContentView(); 
    button=(Button)findViewById(); 
 
    Thread thread=new Thread(new Runnable() 
    { 
      @Override 
      public void run() 
      { 
        ("1111", "111111111"); 
        // TODO Auto-generated method stub 
        Message message=new Message(); 
        =1; 
        (message); 
      } 
    }); 
    (); 
  } 
} 

Second, implement the Runnable interface, let the class implement the Runnable interface, and then propose the run method separately:

package ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
public class Title_Change_Demo extends Activity implements Runnable 
{ 
  public Button button; 
  public LinearLayout my_layout; 
   
  public Handler mHandler=new Handler() 
  { 
    public void handleMessage(Message msg) 
    { 
      switch() 
      { 
      case 1: 
        (.text2); 
        break; 
      default: 
        break;      
      } 
      my_layout.invalidate(); 
      (msg); 
    } 
  }; 
   
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) 
  { 
    (savedInstanceState); 
    setContentView(); 
     
    button=(Button)findViewById(); 
    my_layout=(LinearLayout)findViewById(.my_layout); 
     
    Thread thread=new Thread(this); 
    (); 
  } 
   
  @Override 
  public void run() 
  { 
    ("ok", "111111111"); 
    // TODO Auto-generated method stub 
    Message message=new Message(); 
    =1; 
    (message); 
  } 
} 

The above two implementation methods of Android thread thread thread (must read) are all the content I share with you. I hope you can give you a reference and I hope you can support me more.