Threads are distinguished from thread classes.
In abstract terms, a thread is the smallest unit of CPU scheduling, but the thread always executes the code, which is described in the Threads class (that is, the thread class). In any case, thread is just a class, but its function is to "start a thread and run the user-specified runnable". There are two ways of creating a thread:
- Inherit a thread class to implement its run () method
- Implement runnable directly and construct thread objects with runnable objects
Both methods, finally, call Vmthread.create (this, stacksize) to actually create a thread, the code executed by the thread is specified in the run () method, and the run () method is actually an method of the interface runnable.
Class Thread implements Runnable {//thread also inherits the interface Runnable, and overrides the method run () ... public void Run () { if (target! = null) { target.run ();//Final call to Runnable's Run () method }} ... .
Visible from above: The code that lets the thread execute should be put into the run () method body.
Synchronization between threads is: Wait,notify ()/notifyall (), interrupt (), yield () (yielding processor), join (), and sleep ().
Public final synchronized void join (long Millis) throws interruptedexception { long base = System.currenttimemillis (); Long now = 0; if (Millis < 0) { throw new IllegalArgumentException ("Timeout value is negative"); } if (Millis = = 0) {while (IsAlive ()) { wait (0);//join actually calls wait () (by passing in arguments representing time)} } else { while (IsAlive ()) { long delay = Millis-now; if (delay <= 0) {break ; } Wait (delay); now = System.currenttimemillis ()-Base;}}}
The thread class for Android analytics