Monday, February 24, 2014

Set name of Thread

We can assign a name to a thread by calling its setName(String threadName) method. Inside the thread, we can get the name with: Thread.currentThread().getName().

name of Thread



package com.example.androidthread;

import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {
 
 Button buttonStartA, buttonStart, buttonRun;
 TextView textInfo;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonStartA = (Button)findViewById(R.id.buttonstarta);
  buttonStart = (Button)findViewById(R.id.buttonstart);
  buttonRun = (Button)findViewById(R.id.buttonrun);
  textInfo = (TextView)findViewById(R.id.info);
  
  buttonStartA.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    //start a thread with name
    Thread thread = new Thread(new MyRunnable());
    thread.setName("Thread A");
    thread.start(); //in background thread
   }});
  
  buttonStart.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    Thread thread = new Thread(new MyRunnable());
    thread.start(); //in background thread
   }});
  
  buttonRun.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    Thread thread = new Thread(new MyRunnable());
    thread.setName("another main");
    thread.run(); //in current thread
   }});
 }

 private class MyRunnable implements Runnable {

  @Override
  public void run() {
   
   final String myThreadName = Thread.currentThread().getName();
   
   // check if it's run in main thread, or background thread
   if(Looper.getMainLooper().getThread()==Thread.currentThread()){
    //in main thread
    textInfo.setText("in main thread: " + myThreadName);
   }else{
    //in background thread

    runOnUiThread(new Runnable(){

     @Override
     public void run() {
      textInfo.setText("in background thread: " + myThreadName);
     }
     
    });
   }
  }
  
 }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    <Button
        android:id="@+id/buttonstarta"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="start() with name" />
    <Button
        android:id="@+id/buttonstart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="start()" />
    <Button
        android:id="@+id/buttonrun"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="run()" />
    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


- When button "start() with name" pressed, it show thread name of "Thread A".
- When button "start()" pressed, without name assigned, it show something like "Thread-10807".
- When button "run()" pressed, even a name is assigned to the thread, but the run() method is called in main thread actually. So it show name of "main"



- More example about Thread

No comments: