参考:
下面学习 Java
线程属性
主要内容:
Java
属性浅析- 线程
id
和线程名 - 线程优先级
- 守护线程
Java
属性浅析
Java
属性浅析
Java
属性包括线程 id
,线程名,线程优先级,是否是守护线程等
线程
id
和线程名
id
和线程名
系统赋予每个线程一个单独的 id
,所以通过 id
比较可以判断两个线程是否相同
/**
* Returns the identifier of this Thread. The thread ID is a positive
* <tt>long</tt> number generated when this thread was created.
* The thread ID is unique and remains unchanged during its lifetime.
* When a thread is terminated, this thread ID may be reused.
*
* @return this thread's ID.
* @since 1.5
*/
public long getId() {
return tid;
}
而对于线程名而言,不同的线程可以拥有相同的线程名,设置线程名有两种方式:
- 在创建新线程的时候赋值
- 使用函数
setName
赋值
setName
实现方法如下:
/**
* Changes the name of this thread to be equal to the argument
* <code>name</code>.
* <p>
* First the <code>checkAccess</code> method of this thread is called
* with no arguments. This may result in throwing a
* <code>SecurityException</code>.
*
* @param name the new name for this thread.
* @exception SecurityException if the current thread cannot modify this
* thread.
* @see #getName
* @see #checkAccess()
*/
public final synchronized void setName(String name) {
checkAccess();
if (name == null) {
throw new NullPointerException("name cannot be null");
}
this.name = name;
if (threadStatus != 0) {
setNativeName(name);
}
}
线程优先级
Thread 类定义了 3
个优先级常量:
/**
* The minimum priority that a thread can have.
*/
public final static int MIN_PRIORITY = 1;
/**
* The default priority that is assigned to a thread.
*/
public final static int NORM_PRIORITY = 5;
/**
* The maximum priority that a thread can have.
*/
public final static int MAX_PRIORITY = 10;
默认线程优先级常量为 NROM_PRIORITY
,新建的线程优先级和当前线程优先级相同
使用方法 setPriority
设置优先级,使用方法 getPriority
获取优先级:
/**
* Changes the priority of this thread.
* <p>
* First the <code>checkAccess</code> method of this thread is called
* with no arguments. This may result in throwing a
* <code>SecurityException</code>.
* <p>
* Otherwise, the priority of this thread is set to the smaller of
* the specified <code>newPriority</code> and the maximum permitted
* priority of the thread's thread group.
*
* @param newPriority priority to set this thread to
* @exception IllegalArgumentException If the priority is not in the
* range <code>MIN_PRIORITY</code> to
* <code>MAX_PRIORITY</code>.
* @exception SecurityException if the current thread cannot modify
* this thread.
* @see #getPriority
* @see #checkAccess()
* @see #getThreadGroup()
* @see #MAX_PRIORITY
* @see #MIN_PRIORITY
* @see ThreadGroup#getMaxPriority()
*/
public final void setPriority(int newPriority) {
ThreadGroup g;
checkAccess();
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
if((g = getThreadGroup()) != null) {
if (newPriority > g.getMaxPriority()) {
newPriority = g.getMaxPriority();
}
setPriority0(priority = newPriority);
}
}
/**
* Returns this thread's priority.
*
* @return this thread's priority.
* @see #setPriority
*/
public final int getPriority() {
return priority;
}
Note 1:默认优先级范围在 [1-10]
,但是不同操作系统会有差别,推荐使用优先级常量进行设置
Note 2:当有多个线程就绪时,优先级高的线程先获得处理器资源
守护线程
守护线程又称为后台线程,精灵线程,比如,Java
的垃圾回收线程是守护线程,main
线程不是守护线程。其特征是前台线程全部死亡,守护线程自动死亡
使用方法 isDaemon
判断是否是守护线程;使用方法 setDaemon
进行设置:
/**
* Marks this thread as either a {@linkplain #isDaemon daemon} thread
* or a user thread. The Java Virtual Machine exits when the only
* threads running are all daemon threads.
*
* <p> This method must be invoked before the thread is started.
*
* @param on
* if {@code true}, marks this thread as a daemon thread
*
* @throws IllegalThreadStateException
* if this thread is {@linkplain #isAlive alive}
*
* @throws SecurityException
* if {@link #checkAccess} determines that the current
* thread cannot modify this thread
*/
public final void setDaemon(boolean on) {
checkAccess();
if (isAlive()) {
throw new IllegalThreadStateException();
}
daemon = on;
}
/**
* Tests if this thread is a daemon thread.
*
* @return <code>true</code> if this thread is a daemon thread;
* <code>false</code> otherwise.
* @see #setDaemon(boolean)
*/
public final boolean isDaemon() {
return daemon;
}
Note 1:方法 setDaemon
调用该方法必须在线程就绪之前,否则会抛出IllegalThreadStateException
异常
Note 2:前台线程创建的默认是前台线程,后台线程创建的默认是后台线程