Create New Thread in Android Part – 2
In this video, we see How to create a new thread in android? The code to be executed in that thread needs to be placed within the Run() method of a Runnable instance.A new Thread object then needs to be created, passing through a reference to the Runnable instance to the constructor. Finally, the start() method of the thread object needs to be called to start the thread running.
Example:
public void buttonClick(View view)
{
Runnable runnable = new Runnable() {
public void run() {
long endTime = System.currentTimeMillis()
+ 20*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime –
System.currentTimeMillis());
} catch (Exception e) {}
}
}
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}