Thread Usage:
(i) the way to use inheritance (high coupling, not recommended)
Define a thread, create a new class to inherit from thread, then rewrite the parent class's run () method and add time-consuming logic inside it,
class extends thread{ @Override publicvoid run () { // Add specific logic ... }}
To start this thread, simply new out the instance of MyThread and then call its start () method so that the code in the Run () method runs in the child thread. As follows
New MyThread (). Start ();
(ii) How the interface is used
Define a thread, create a new class, implement the Runnable interface,
class Implements runnable{ @Override publicvoid run () { // Add specific logic ... }}
When starting a thread, you need to pass an instance of the defined class as a parameter, into the thread's constructor method, and then call the thread's start () method.
MyThread mythread=New MyThread (); //new Thread (myThread). Start (); Thread objthread=new thread (myThread); Objthread.start ();
(iii) The use of anonymous classes to achieve
New Thread (new Runnable () { @Override publicvoid run () { // Add specific logic ... }). Start ();
Basic usage of Android threads