📅  最后修改于: 2020-11-15 03:09:41             🧑  作者: Mango
java.lang.Thread类是程序中的执行线程。 Java虚拟机允许应用程序具有多个并发运行的执行线程。以下是关于线程的要点-
每个线程都有一个优先级。高优先级的线程优先于低优先级的线程执行
每个线程可能会也可能不会被标记为守护程序。
有两种方法可以创建新的执行线程。一种是将一个类声明为Thread的子类,然后,
创建线程的另一种方法是声明一个实现Runnable接口的类
以下是java.lang.Thread类的声明-
public class Thread
extends Object
implements Runnable
以下是java.lang.Thread类的字段-
static int MAX_PRIORITY-这是线程可以具有的最大优先级。
static int NORM_PRIORITY-这是分配给线程的默认优先级。
Sr.No. | Constructor & Description |
---|---|
1 |
Thread() This allocates a new Thread object. |
2 |
Thread(Runnable target) This allocates a new Thread object. |
3 |
Thread(Runnable target, String name) This allocates a new Thread object. |
4 |
Thread(String name) This constructs allocates a new Thread object. |
5 |
Thread(ThreadGroup group, Runnable target) This allocates a new Thread object. |
6 |
Thread(ThreadGroup group, Runnable target, String name) This allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group. |
7 |
Thread(ThreadGroup group, Runnable target, String name, long stackSize) This allocates a new Thread object so that it has target as its run object, has the specified name as its name, belongs to the thread group referred to by group, and has the specified stack size. |
8 |
Thread(ThreadGroup group, String name) This allocates a new Thread object. |
Sr.No. | Method & Description |
---|---|
1 | static int activeCount()
This method returns the number of active threads in the current thread’s thread group. |
2 | void checkAccess()
This method determines if the currently running thread has permission to modify this thread. |
3 |
protected Object clone() This method returns a clone if the class of this object is Cloneable. |
4 | static Thread currentThread()
This method returns a reference to the currently executing thread object. |
5 | static void dumpStack()
This method prints a stack trace of the current thread to the standard error stream. |
6 | static int enumerate(Thread[] tarray)
This method copies into the specified array every active thread in the current thread's thread group and its subgroups. |
7 | static Map This method returns a map of stack traces for all live threads. |
8 | ClassLoader getContextClassLoader()
This method returns the context ClassLoader for this Thread. |
9 | static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
This method returns the default handler invoked when a thread abruptly terminates due to an uncaught exception. |
10 | long getId()
This method returns the identifier of this Thread. |
11 | String getName()
This method returns this thread's name. |
12 | int getPriority()
This method Returns this thread's priority. |
13 | StackTraceElement[] getStackTrace()
This method returns an array of stack trace elements representing the stack dump of this thread. |
14 | Thread.State getState()
This method returns the state of this thread. |
15 | ThreadGroup getThreadGroup()
This method returns the thread group to which this thread belongs. |
16 | Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
This method returns the handler invoked when this thread abruptly terminates due to an uncaught exception. |
17 | static boolean holdsLock(Object obj)
This method returns true if and only if the current thread holds the monitor lock on the specified object. |
18 | void interrupt()
This method interrupts this thread. |
19 | static boolean interrupted()
This method tests whether the current thread has been interrupted. |
20 | boolean isAlive()
This method tests if this thread is alive. |
21 | boolean isDaemon()
This method tests if this thread is a daemon thread. |
22 | boolean isInterrupted()
This method tests whether this thread has been interrupted. |
23 | void join()
Waits for this thread to die. |
24 | void join(long millis)
Waits at most millis milliseconds for this thread to die. |
25 | void join(long millis, int nanos)
Waits at most millis milliseconds plus nanos nanoseconds for this thread to die. |
26 | void run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns |
27 | void setContextClassLoader(ClassLoader cl)
This method sets the context ClassLoader for this Thread. |
28 | void setDaemon(boolean on)
This method marks this thread as either a daemon thread or a user thread. |
29 | static void setDefaultUncaughtExceptionHandler (Thread.UncaughtExceptionHandler eh)
This method set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread. |
30 | void setName(String name)
This method changes the name of this thread to be equal to the argument name. |
31 | void setPriority(int newPriority)
This method changes the priority of this thread. |
32 | void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
This method set the handler invoked when this thread abruptly terminates due to an uncaught exception. |
33 | static void sleep(long millis)
This method causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. |
34 | static void sleep(long millis, int nanos)
This method causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers. |
35 | void start()
This method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. |
36 | String toString()
This method Returns a string representation of this thread, including the thread's name, priority, and thread group. |
37 | static void yield()
This method causes the currently executing thread object to temporarily pause and allow other threads to execute. |
此类从以下类继承方法-