Java多线程03_线程状态、优先级、用户线程和守护线程

Java多线程03_线程状态、优先级、用户线程和守护线程


在这里插入图片描述


线程方法:

setPriority()				更改线程优先级
static void sleep()			线程休眠
void join()					插队
static void yield()			礼让
void interrupt()			中断线程
boolean isAlive()			是否存活

停止线程:

不推荐JDK提供的stop()、destory()方法 【已废弃】

推荐让线程正常停止,不要死循环!

建议使用一个标志位进行终止,当flag=false,则终止线程运行

public class TestStop implements Runnable{

	private boolean flag = true;
	
	@Override
	public void run() {
		int i = 0;
		while(flag) {
			System.out.println("run...Thread"+i++);
		}
	}
	
	//设置公开方法利用标志位停止线程
	public void stopThread() {
		this.flag = false;
		System.out.println("线程停止了");
	}
	
	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) {
				testStop.stopThread();
			}
		}
		
	}
	
}

线程休眠:

sleep方法存在异常:InterruptedException

sleep时间结束,线程进入就绪状态

每个对象都有一个锁,sleep不会释放锁

Thread.sleep(1000);

线程礼让:

让当前执行的线程暂停,但不阻塞

将线程从运行状态转为就绪状态

礼让不一定成功,看cpu心情

Thread.yield();

Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞(插队)

main线程和Thread线程交替执行,当main线程执行到200时,Thread线程插队,一直到执行结束,main线程才能重新执行

package com.blu.demo1;

public class TestJoin implements Runnable{

	@Override
	public void run() {
		for(int i = 0;i < 1000;i++) {
			System.out.println("vip线程正在执行....."+i);
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		TestJoin testJoin = new TestJoin();
		Thread thread = new Thread(testJoin);
		thread.start();
		
		for(int i = 0;i < 1000;i++) {
			if(i==200) {
				thread.join();
			}
			System.out.println("main线程正在执行....."+i);
		}
	}

}

getState()监测线程状态:

返回枚举类型:

NEW	
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
TERMINATED

线程优先级:

线程优先级用1-10之间的整数表示,1为最低优先级,10为最高优先级

主线程优先级默认为5,且不可修改

枚举
Thread.MAX_PRIORITY = 10
Thread.NORM_PRIORITY = 5
Thread.MIN_PRIORITY = 1

优先级高不一定先执行,由CPU调度决定

public class TestPriority {
	
	public static void main(String[] args) {
		//主线程优先级
		System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
		MyPriority myPriority = new MyPriority();
		Thread t1 = new Thread(myPriority);
		Thread t2 = new Thread(myPriority);
		Thread t3 = new Thread(myPriority);
		Thread t4 = new Thread(myPriority);
		Thread t5 = new Thread(myPriority);
		Thread t6 = new Thread(myPriority);
		//设置优先级
		t1.start();
		t2.setPriority(1);
		t3.setPriority(4);
		t4.setPriority(Thread.MAX_PRIORITY);
		t5.setPriority(5);
		t6.setPriority(7);
		t2.start();
		t3.start();
		t4.start();
		t5.start();
		t6.start();
	}
	
}

class MyPriority implements Runnable{

	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
	}
	
}

守护线程

线程分为用户线程(比如main)守护线程(比如gc)

虚拟机必须确保用户线程执行完毕,不用等待守护线程执行完毕

当只剩下守护线程时,JVM就会退出,只要存在一个用户线程,JVM就不会退出

public class TestDaemon {
	
	public static void main(String[] args) {
		God god = new God();
		You you = new You();
		Thread thread = new Thread(god);
		thread.setDaemon(true);
		thread.start();
		new Thread(you).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<=100;i++) {
			System.out.println("你开心地活了"+i+"年");
		}
		System.out.println("GoodBye,World!");
	}
	
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页