Java Thread (i)

Source: Internet
Author: User

Thread class Runnable interface Run method

Creation of Threads:

Thread ()

Thread (String name)

Thread (Runnable target)

Thread (Runnable target,string name)

Methods of threading:

Start: void Start ()

Dormancy:

static void sleep (long Millis)

static void sleep (long millis, int nanos)

Causes other threads to wait for the current thread to terminate

void join () must wait for the current thread to terminate

void join (Long Millis) the longest waiting time

void join (long millis, int nanos) time can be accurate to nanoseconds

Current running thread frees processor resources

static void Yield ()

get thread Reference: returns the currently running thread reference

Static Thread CurrentThread ()

How to properly stop a thread in Java

Instead of using the Stop () method, use the exit flag, such as the stop condition of the while, to make the thread complete and clean up

Mutual exclusion of threads: critical data can only be used by one thread at a time

Private Final Object Lockobj = object ();

Synchronized (lockobj) {

..................

}

thread synchronization: is a process of interaction between several threads, because some conditions do not have the status of a thread waiting, at some time due to certain conditions again, send a message, wake all threads

Wait ()/notify ()/notifyall () These three methods belong to an object

The thread picks up the object.wait () and releases the lock so that other threads can get the lock

volatile guarantees that the thread can read the values written by other threads correctly (visibility issues), and if no volatile is declared, it is possible that the visibility issue cannot be read correctly

Java Thread (i)

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: info-contact@alibabacloud.com 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.