Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created January 25, 2012 02:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daichan4649/1674289 to your computer and use it in GitHub Desktop.
Save daichan4649/1674289 to your computer and use it in GitHub Desktop.
AsyncTaskTest(API Level under 13 ,over 13)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.asynctask"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="12" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="execute" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="executeOnExecutor" />
</LinearLayout>
package test.asynctask;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
private static final String LOG_TAG = "AsyncTaskTest";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
testAsyncTaskExecute();
}
});
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
testAsyncTaskExecuteOnExecutor();
}
});
}
private void testAsyncTaskExecute() {
AsyncTask<Void, Void, Void> task1 = new TestAsyncTask();
AsyncTask<Void, Void, Void> task2 = new TestAsyncTask();
task1.execute();
task2.execute();
}
private void testAsyncTaskExecuteOnExecutor() {
AsyncTask<Void, Void, Void> task1 = new TestAsyncTask();
AsyncTask<Void, Void, Void> task2 = new TestAsyncTask();
task1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
task2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private static class TestAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
long tId = Thread.currentThread().getId();
Log.d(LOG_TAG, "[doInBackground]start " + tId);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d(LOG_TAG, "[doInBackground]end " + tId);
return null;
}
}
}
@dentex
Copy link

dentex commented Sep 7, 2013

long tId = Thread.currentThread().getId(); is exactly what I was looking for. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment