Java 线程 4
来源:互联网 发布:linux中tail命令 编辑:程序博客网 时间:2024/06/16 08:07
参考:
Java 线程 0 - 前言
下面学习 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:前台线程创建的默认是前台线程,后台线程创建的默认是后台线程
- java 线程4
- Java 线程 4
- Java线程4:线程的生命周期
- Java线程(4)线程池
- Java线程(4):线程状态的转换
- Java线程-4:线程状态的转换
- java#4 控制线程(join),后台线程,线程睡眠(sleep),线程让步(yield),线程优先级
- Java笔记(4)-线程、Thread、Runable、开子线程、线程运行状态、线程同步、线程死锁
- java线程技术4_Volatile
- Java中的线程(4)
- Java多线程(4)线程组
- 【Java】4种线程池
- Java线程:什么是线程
- Java线程:线程池
- java线程--线程退出
- JAVA-线程/线程锁
- Java线程:什么是线程
- Java线程:线程中断
- a64_7.1android平板更改状态栏底部导航栏navgationBar颜色
- ios WKWebView 与 JS 交互实战技巧
- swing重绘按钮为任意形状
- 使用GZIP解压缩数据,以及解决中文乱码
- tensorflow c/c++库使用方法
- Java 线程 4
- 拉里·埃里森亲自支招,数据库自动化之后,DBA何去何从?
- 关于ImageLoader的使用将图片改变为圆形
- 1190: [HNOI2007]梦幻岛宝珠
- Vue + ElementUI 手撸后台管理网站基本框架(二)权限控制
- 蓝桥杯:世纪末的星期天
- 机器学习算法之逻辑回归
- 关于python的基础知识17 -- 列表、数组和矩阵
- 创投日报:10月31日收录投融资项目14起