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)