【多线程
1. 观测线程状态
Thread.State 可以处于一下几种状态
- NEW:尚未启动的线程处于此种状态
- RUNABLE:在Java虚拟机中执行的线程处于此种状态
- BLOCKED:被阻塞等待监视器锁定的线程处于此种状态
- WAITING:正在等待另一个线程执行特定动作的线程处于此种状态
- TIMED_WAITING:正在等待另一个线程执行动作达到指定等待时间的线程处于此种状态
- TERMINATED:已退出的线程处于此种状态
//观察测试线程的状态
public class TestState {public static void main(String[] args) {Thread thread = new Thread(()->{for (int i = 0; i < 10; i++) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("//");});//观察状态Thread.State state = thread.getState();System.out.println(state);//观察启动后状态thread.start();state = thread.getState();System.out.println(state);while (state!= Thread.State.TERMINATED){//只要线程不终止,就一直检测线程状态state = thread.getState();//更新状态System.out.println(state);}//一旦进入死亡状态,就不能再次重启}
}
2. 线程优先级
- Java提供一个线程调度器来监控程序中启动进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行。
- 线程优先级用数字表示,范围从1~10。Thread.MIN_PRIORITY=1、Thread.MAX_PRIORITY=10、Thread.NORM_PRIORITY=5
- 使用getPriority().setPriority(int )改变或者获取线程优先级
- 优先级高的不一定先执行
//测试现场优先级
public class TestPrioroty{public static void main(String[] args) {//输出主函数的优先级System.out.println(Thread.currentThread().getName()+"==="+Thread.currentThread().getPriority());MyPriority myPriority = new MyPriority();Thread thread1 = new Thread(myPriority);Thread thread2 = new Thread(myPriority);Thread thread3 = new Thread(myPriority);Thread thread4 = new Thread(myPriority);Thread thread5 = new Thread(myPriority);thread1.start();thread2.setPriority(7);thread2.start();thread3.setPriority(2);thread3.start();thread4.setPriority(Thread.NORM_PRIORITY);thread4.start();}
}class MyPriority implements Runnable{@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"==="+Thread.currentThread().getPriority());}
}
3.守护(daemon)线程
- 线程分为用户线程和守护线程
- 虚拟机必须确保用户线程执行完毕
- 虚拟机不用等待守护线程执行完毕
- 守护线程如:后台操作日志、监控内存、垃圾回收等
//例:人是短暂存在的(用户线程),神是永恒的(守护线程),当人离开后,神也会停止======虚拟机不会等待守护线程执行完毕!
public class TestDaemon {public static void main(String[] args) {Person person = new Person();God god = new God();Thread thread = new Thread(god);thread.setDaemon(true);//默认是false表示用户线程,正常的线程都是用户线程。在此表示设置为守护线程thread.start();new Thread(person).start();}
}
//人是短暂的
class Person implements Runnable{@Overridepublic void run() {for (int i = 0; i < 30000; i++) {System.out.println("i am person");}}
}
//神是永恒的
class God implements Runnable{@Overridepublic void run() {while (true){System.out.println("i am god");}}
}
【多线程
1. 观测线程状态
Thread.State 可以处于一下几种状态
- NEW:尚未启动的线程处于此种状态
- RUNABLE:在Java虚拟机中执行的线程处于此种状态
- BLOCKED:被阻塞等待监视器锁定的线程处于此种状态
- WAITING:正在等待另一个线程执行特定动作的线程处于此种状态
- TIMED_WAITING:正在等待另一个线程执行动作达到指定等待时间的线程处于此种状态
- TERMINATED:已退出的线程处于此种状态
//观察测试线程的状态
public class TestState {public static void main(String[] args) {Thread thread = new Thread(()->{for (int i = 0; i < 10; i++) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("//");});//观察状态Thread.State state = thread.getState();System.out.println(state);//观察启动后状态thread.start();state = thread.getState();System.out.println(state);while (state!= Thread.State.TERMINATED){//只要线程不终止,就一直检测线程状态state = thread.getState();//更新状态System.out.println(state);}//一旦进入死亡状态,就不能再次重启}
}
2. 线程优先级
- Java提供一个线程调度器来监控程序中启动进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行。
- 线程优先级用数字表示,范围从1~10。Thread.MIN_PRIORITY=1、Thread.MAX_PRIORITY=10、Thread.NORM_PRIORITY=5
- 使用getPriority().setPriority(int )改变或者获取线程优先级
- 优先级高的不一定先执行
//测试现场优先级
public class TestPrioroty{public static void main(String[] args) {//输出主函数的优先级System.out.println(Thread.currentThread().getName()+"==="+Thread.currentThread().getPriority());MyPriority myPriority = new MyPriority();Thread thread1 = new Thread(myPriority);Thread thread2 = new Thread(myPriority);Thread thread3 = new Thread(myPriority);Thread thread4 = new Thread(myPriority);Thread thread5 = new Thread(myPriority);thread1.start();thread2.setPriority(7);thread2.start();thread3.setPriority(2);thread3.start();thread4.setPriority(Thread.NORM_PRIORITY);thread4.start();}
}class MyPriority implements Runnable{@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"==="+Thread.currentThread().getPriority());}
}
3.守护(daemon)线程
- 线程分为用户线程和守护线程
- 虚拟机必须确保用户线程执行完毕
- 虚拟机不用等待守护线程执行完毕
- 守护线程如:后台操作日志、监控内存、垃圾回收等
//例:人是短暂存在的(用户线程),神是永恒的(守护线程),当人离开后,神也会停止======虚拟机不会等待守护线程执行完毕!
public class TestDaemon {public static void main(String[] args) {Person person = new Person();God god = new God();Thread thread = new Thread(god);thread.setDaemon(true);//默认是false表示用户线程,正常的线程都是用户线程。在此表示设置为守护线程thread.start();new Thread(person).start();}
}
//人是短暂的
class Person implements Runnable{@Overridepublic void run() {for (int i = 0; i < 30000; i++) {System.out.println("i am person");}}
}
//神是永恒的
class God implements Runnable{@Overridepublic void run() {while (true){System.out.println("i am god");}}
}