java.lang.Thread
- Get current thread
public static native Thread currentThread(); - name - thread name
private volatile char name[]; - priority
private int priority; public final static int MIN_PRIORITY = 1; public final static int MAX_PRIORITY = 10; // default public final static int NORM_PRIORITY = 5; - daemon thread - Java has 2 types of thread, user thread and daemon thread.
private boolean daemon = false; // use to set thread as daemon public final void setDaemon(boolean on) - target - the object whose run() method gets called.
private Runnable target; - start - start a thread to call the
runmethord. - run - called the run method of
target - sleep - the sleepping thread will not release the lock, code in synchronized block will still be locked.
public static native void sleep(long millis) throws InterruptedException; public static void sleep(long millis, int nanos) - yield
- join
public final void join() throws InterruptedException public final synchronized void join(long millis) public final synchronized void join(long millis, int nanos) interrupt
public void interrupt() // Tests if the current thread has been interrupted. And clear interrupt status. public static boolean interrupted() // Tests whether this thread has been interrupted. It will not clear interrupt status. public boolean isInterrupted() private native boolean isInterrupted\(boolean ClearInterrupted);thread state
public enum State { NEW, RUNNABLE, // A thread in the blocked state is waiting for a monitor lock // to enter a synchronized block/method BLOCKED, // Object.wait with no timeout // Thread.join with no timeout WAITING, // Thread.sleep // Object.wait with timeout // Thread.join with timeout TIMED_WAITING, TERMINATED; }
from:Java並發編程:Thread類的使用