forked from artem-zinnatullin/Android-AsyncTask-Executor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsageExampleActivity.java
More file actions
70 lines (59 loc) · 1.86 KB
/
UsageExampleActivity.java
File metadata and controls
70 lines (59 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package ru.artemzin.android.asynctaskexecutor;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class UsageExampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestTask task0 = new TestTask("task0");
TestTask task1 = new TestTask("task1");
TestTask task2 = new TestTask("task2");
TestTask task3 = new TestTask("task3");
TestTask task4 = new TestTask("task4");
// Sleep for 5 seconds
AsyncTaskExecutor.executeConcurrently(task0, Long.valueOf(5000));
// Sleep for 1 second
AsyncTaskExecutor.executeConcurrently(task1, Long.valueOf(1000));
// Sleep for 0 second
AsyncTaskExecutor.executeConcurrently(task2, Long.valueOf(0));
// Sleep for 2 second
AsyncTaskExecutor.executeConcurrently(task3, Long.valueOf(2000));
// Sleep for 0.5 second
AsyncTaskExecutor.executeConcurrently(task4, Long.valueOf(500));
// As you can see, finish order: task2, task4, task1, task3, task0
// Concurrently on any Android version!
}
private void showMessage(String message) {
Log.d("AsyncTaskConcurrently", message);
Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
toast.show();
}
/**
* Task for test
* @author Artem Zinnatullin (artem.zinnatullin@gmail.com)
*/
private class TestTask extends AsyncTask<Long, Integer, Boolean> {
private String name;
public TestTask(String name) {
this.name = name;
}
@Override
protected Boolean doInBackground(Long... params) {
try {
Thread.sleep(params[0]);
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
showMessage(this.name + " finished");
}
}
}
}