суббота, 28 февраля 2015 г.
Threads. Create and run thread
A thread is a concurrent unit of execution and uing for processing long-time tasks.
Each Android application has at least one thread running when it is started, the main thread, in the main ThreadGroup. The runtime keeps its own threads in the system thread group. If need running background long-time task (for example downloading files from internet), must use background thread.
There are two ways to execute code in a new thread. You can either subclass Thread and overriding its run() method,
public void run();
or construct a new Thread and pass a Runnable to the constructor. In either case, the start() method must be called to actually execute the new Thread:
public Thread(Runnable runnable);
Where Runnable represents a command that can be executed. Often used to run code in a different Thread.
To start the flow you need to call start () method of class Thread. When the flow will cause a run () method of the object Runnable.
Thread background = new Thread(new Runnable() {
…
}
Create new project and write following code in layout file:
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/button_start"
android:layout_height="wrap_content"
android:text="Start"
android:onClick="onClick"
android:layout_width="fill_parent"/>
</LinearLayout>
As for the lon-time task we will cycle on 100 steps. At each iteration of the loop thread will sleep for 100 milliseconds and send information in LogCat (see Android Studio or Eclipse ToolWindow Android DDMS) message:
Thread.sleep(100);
Log.i("Thread", String.format("End task #%d", count));
Complete the quest will be 10 seconds. Thread will run when you press Start.
MainActivity class:
MainActivity.java
package com.samples.async.handler_simple;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity implements View.OnClickListener {
// Maximum steps for loop
private static final int MAX_COUNT = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onClick(View v) {
// Create Thread object
Thread background = new Thread(new Runnable() {
public void run() {
int count = 0;
// loop
while (count < MAX_COUNT) {
try {
// 100 ms delay
Thread.sleep(100);
// Send message in LogCat
Log.i("Thread", String.format("End task #%d", count));
count++;
}
catch (InterruptedException e) {
Log.e("ERROR", "Thread Interrupted");
}
}
}
});
// run thread
background.start();
}
}
If you run the program to execute and open tab in Eclipse LogCat, we see our work created by the flow.
When the job created thread does not block the user interface, it does not hang, and in parallel with the user to perform the task can perform other actions in the main stream.
However, the program does not know when the job is over, because background thread does not interact with the main stream. Therefore, the program is necessary to implement a mechanism to send messages.
Подписаться на:
Сообщения (Atom)