java后台进程和线程优先级
2013-11-14 09:09
337 查看
1. 后台线程:处于后台运行,任务是为其他线程提供服务。也称为“守护线程”或“精灵线程”。JVM的垃圾回收就是典型的后台线程。
特点:若所有的前台线程都死亡,后台线程自动死亡。
设置后台线程:Thread对象setDaemon(true);
setDaemon(true)必须在start()调用前。否则出现IllegalThreadStateException异常;
前台线程创建的线程默认是前台线程;
判断是否是后台线程:使用Thread对象的isDaemon()方法;
并且当且仅当创建线程是后台线程时,新线程才是后台线程。
例子:
class Daemon implements Runnable{
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("Daemon -->" + i);
}
}
}
public class DaemonDemo {
public static void main(String[] args) {
/* Thread cThread = Thread.currentThread();
System.out.println(cThread.isAlive());
//cThread.setDaemon(true);
System.out.println(cThread.isDaemon());*/
Thread t = new Thread(new Daemon());
System.out.println(t.isDaemon());
for (int i = 0; i < 10; i++) {
System.out.println("main--" + i);
if(i == 5){
t.setDaemon(true);
t.start();
}
}
}
}
2,线程的优先级:
每个线程都有优先级,优先级的高低只和线程获得执行机会的次数多少有关。
并非线程优先级越高的就一定先执行,哪个线程的先运行取决于CPU的调度;
默认情况下main线程具有普通的优先级,而它创建的线程也具有普通优先级。
Thread对象的setPriority(int x)和getPriority()来设置和获得优先级。
MAX_PRIORITY
: 值是10
MIN_PRIORITY
: 值是1
NORM_PRIORITY
: 值是5(主方法默认优先级)
注意:每个线程默认的优先级都与创建他的父线程的优先级相同,在在默认的情况下,
main线程具有普通优先级,由main线程创建的子线程也具有普通优先级
例子:
class Priority implements Runnable{
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("Priority-- " + i);
}
}
}
public class PriorityDemo {
public static void main(String[] args) {
/**
* 线程的优先级在[1,10]之间
*/
Thread.currentThread().setPriority(3);
System.out.println("main= " + Thread.currentThread().getPriority());
/*
* public final static int MIN_PRIORITY = 1;
* public final static int NORM_PRIORITY = 5;
* public final static int MAX_PRIORITY = 10;
* */
System.out.println(Thread.MAX_PRIORITY);
//===============================================
Thread t = new Thread(new Priority());
for (int i = 0; i < 200; i++) {
System.out.println("main" + i);
if(i == 50){
t.start();
t.setPriority(10);
}
System.out.println("-------------------------"+t.getPriority());
}
}
}
特点:若所有的前台线程都死亡,后台线程自动死亡。
设置后台线程:Thread对象setDaemon(true);
setDaemon(true)必须在start()调用前。否则出现IllegalThreadStateException异常;
前台线程创建的线程默认是前台线程;
判断是否是后台线程:使用Thread对象的isDaemon()方法;
并且当且仅当创建线程是后台线程时,新线程才是后台线程。
例子:
class Daemon implements Runnable{
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("Daemon -->" + i);
}
}
}
public class DaemonDemo {
public static void main(String[] args) {
/* Thread cThread = Thread.currentThread();
System.out.println(cThread.isAlive());
//cThread.setDaemon(true);
System.out.println(cThread.isDaemon());*/
Thread t = new Thread(new Daemon());
System.out.println(t.isDaemon());
for (int i = 0; i < 10; i++) {
System.out.println("main--" + i);
if(i == 5){
t.setDaemon(true);
t.start();
}
}
}
}
2,线程的优先级:
每个线程都有优先级,优先级的高低只和线程获得执行机会的次数多少有关。
并非线程优先级越高的就一定先执行,哪个线程的先运行取决于CPU的调度;
默认情况下main线程具有普通的优先级,而它创建的线程也具有普通优先级。
Thread对象的setPriority(int x)和getPriority()来设置和获得优先级。
MAX_PRIORITY
: 值是10
MIN_PRIORITY
: 值是1
NORM_PRIORITY
: 值是5(主方法默认优先级)
注意:每个线程默认的优先级都与创建他的父线程的优先级相同,在在默认的情况下,
main线程具有普通优先级,由main线程创建的子线程也具有普通优先级
例子:
class Priority implements Runnable{
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("Priority-- " + i);
}
}
}
public class PriorityDemo {
public static void main(String[] args) {
/**
* 线程的优先级在[1,10]之间
*/
Thread.currentThread().setPriority(3);
System.out.println("main= " + Thread.currentThread().getPriority());
/*
* public final static int MIN_PRIORITY = 1;
* public final static int NORM_PRIORITY = 5;
* public final static int MAX_PRIORITY = 10;
* */
System.out.println(Thread.MAX_PRIORITY);
//===============================================
Thread t = new Thread(new Priority());
for (int i = 0; i < 200; i++) {
System.out.println("main" + i);
if(i == 50){
t.start();
t.setPriority(10);
}
System.out.println("-------------------------"+t.getPriority());
}
}
}
相关文章推荐
- java后台进程和线程优先级
- java并发(2)-优先级,让步,后台线程
- java多进程 -CD7-孙鑫-(1)-程序/进程/线程/设置后台线程/将当前线程暂停
- Java多线程 小知识 守护线程(后台线程)setDaemon、join方法、优先级、yield方法
- java 线程返回值,优先级,后台线程 示例代码
- java种前后台线程和进程的关系
- JAVA 用户线程,后台进程
- JAVA基础初探(十四)多线程(线程与进程概述、线程的实现、状态、常用方法、优先级、生命周期)
- Java 多线程(三) 线程的生命周期及优先级
- java多线程总结二:后台线程(守护线程)
- Java多线程后台线程
- Java线程:线程的调度-优先级
- java进程和线程
- java多线程之线程的优先级
- linux java 查找进程中的线程
- 黑马程序员--读写字节数组,随机读写流,集合IO的思维导图,多线程部分,单例设计模式,线程和进程的概念,Java中的线程的创建方式,线程的随机性,线程的状态图,多线程操作共享数据的安全性,死锁
- <JAVA学习笔记4>——进程、线程简单介绍
- java中查看进程中线程使用情况
- linux 后台启动java jar(守护线程)
- java 线程后台运行示例