The thread class for Android analytics

Source: Internet
Author: User

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:

    1. Inherit a thread class to implement its run () method
    2. 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) {   ();//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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.