N11 多线程(二)
作者:迷恋
一、线程状态
(bilibili狂神说Java,www.kuangstudy.com,一同学习,一起进步)
线程中断或者结束,一旦进入死亡状态,就不能再次启动
观察线程状态:
实例:
package demo35;
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("///");
});
//观察状态
Thread.State state = thread.getState();
System.out.println(state);//New
//观察启动后
thread.start();//启动线程
state = thread.getState();//获取线程状态
System.out.println(state);//Run
while(state!=Thread.State.TERMINATED){//只要线程不终止,就一直输出状态
Thread.sleep(100);
state = thread.getState();//更新线程状态
System.out.println(state);
}
}
}
运行结果:
二、线程停止
- 不推荐使用JDK提供的stop()、destroy()方法。【已废弃】
- 推荐线程自己停止下来。
- 建议使用一个标志位进行终止变量,当flag=false,则终止线程运行。
实例:
package demo34;
public class TestStop implements Runnable{
//1、设置一个标识位
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag){
System.out.println("run...Thread"+i++);
}
}
//2、设置一个公开的方法停止线程,转换标志位
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main"+i);
if (i==900){
//调用stop方法切换标志位,让线程停止
testStop.stop();
System.out.println("线程停止了");
}
}
}
}
运行结果:
当main线程运行到i=900的时候就调用了stop方法,切换标志位使得分线程停止了。
三、线程休眠
- sleep(时间)指定当前线程堵塞的毫秒数。
- sleep存在异常InterruptedException。
- sleep时间达到后线程进入就绪状态。
- sleep可以模拟网络延时,倒计时等。
- 每一个对象都有一个锁,sleep不会释放锁;
实例:
package demo34;
public class TestSleep implements Runnable{
private int i;
@Override
public void run() {
while (true){
System.out.println("我一秒运行一次"+i++);
try {
Thread.sleep(1000);//单位为毫秒,1000毫秒=1秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Thread(new TestSleep()).start();
}
}
运行结果:
每一次执行都会相隔一秒钟
四、线程礼让
- 礼让线程,让当前正在执行的线程暂停,但不阻塞。
- 将线程从运行状态转为就绪状态
- 让cpu重新调度,礼让不一定成功!看cpu心情~
假设有线程A与线程B,当A线程进入执行时,如果此时进行礼让,A线程就会出来暂停执行,此时CPU重新调度,有可能B进入执行,也有可能还是A线程进入执行。
语句:
Thread.yield();
五、线程强制执行
Join
Join合并线程,待此线程执行完成后,再执行其他线程,其他线程堵塞(可以想象成插队)
通常用于在main()主线程内,等待其它线程完成再结束main()主线程。
实例:
package demo34;
public class TestJoin {
public static void main(String[] args) throws InterruptedException {
Demo2 demo2 = new Demo2();
Thread thread = new Thread(demo2);
thread.start();
for (int i = 0; i < 1000; i++) {
thread.join();
System.out.println("Main执行"+i);
}
}
}
class Demo2 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("VIP插队"+i);
}
}
}
运行结果:
VIP线程执行,Main线程堵塞、等待VIP线程执行完才继续执行。
六、线程优先级
- Java 提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行。
- 线程的优先级用数字表示,范围从1~10
- Thread.MIN_PRIORITY=1;
- Thread.MAX_PRIORITY=10;
- Thread.NORM_PRIORITY=5;
- 使用以下方式改变或获取优先级
- getPriority()
- setPriority(int XXX)
实例:
package demo36;
public class TestPriority {
public static void main(String[] args) {
//主线程默认优先级
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
PriorityDemo priorityDemo = new PriorityDemo();
Thread t1 = new Thread(priorityDemo);
Thread t2 = new Thread(priorityDemo);
Thread t3 = new Thread(priorityDemo);
Thread t4 = new Thread(priorityDemo);
Thread t5 = new Thread(priorityDemo);
//先设置优先级,再启动
t1.setPriority(1);
t2.setPriority(4);
t3.setPriority(8);
t4.setPriority(7);
t5.setPriority(10);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class PriorityDemo implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
}
运行结果:
但是也有可能出现下面这类没有按照优先级运行的情况:
这是因为:
优先级低只是意味着获得调度的概率低,并不是优先级低就不会在优先级高之前被调用,这取决于CPU的调度。
七、守护线程
线程分为用户线程和守护线程
虚拟机必须确保用户线程执行完毕
虚拟机不用等待守护线程执行完毕
如,后台记录操作日志,监控内存,垃圾回收等待…
实例:
package demo37;
public class TestDaemon {
public static void main(String[] args) {
You you = new You();
God god = new God();
Thread thread1 = new Thread(god);
//设置为守护线程
thread1.setDaemon(true);//默认是false:用户线程
thread1.start();
Thread thread2 = new Thread(you);
thread2.start();
}
}
//上帝守护线程
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("上帝守护着你");
}
}
}
//你,用户线程
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("你开心地活着");
}
System.out.println("goodbye!world");
}
}
运行结果:
当用户线程结束后不久,守护线程也就结束了。