Runnable in background thread |
In this exercise, we need to check if the current thread is the main thread, or called UI thread. Using the code:
Looper.getMainLooper().getThread()==Thread.currentThread()
If it's true, means it's in main thread, otherwise it's in background thread.
The example show how to implement a Thread with Runnable object. And call its start() method to starts the new Thread of execution. Also notice that if you call its run() method, it will calls the run() method of the Runnable object directly, in current 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 buttonStart, buttonRun;
TextView textInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = (Button)findViewById(R.id.buttonstart);
buttonRun = (Button)findViewById(R.id.buttonrun);
textInfo = (TextView)findViewById(R.id.info);
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.run(); //in current thread
}});
}
private class MyRunnable implements Runnable {
@Override
public void run() {
// 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");
}else{
//in background thread
runOnUiThread(new Runnable(){
@Override
public void run() {
textInfo.setText("in background thread");
}
});
}
}
}
}
<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/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>
More example about Thread:
- Set name of Thread
- Share object between threads with synchronized methods
- Share object between threads with synchronized Statements
- Synchronized Statements with separate lock object, I
- Synchronized Statements with separate lock object, II
- Example of using Lock/ReentrantLock
- Solve Producer–consumer problem with wait() and notifyAll()
ليست هناك تعليقات:
إرسال تعليق