上次我们分享了线程的基本知识,这次分享下Thread的常用方法
目录
前言
本次分享了Thread类的常用方法
一、线程状态
二、线程方法介绍
1.线程睡眠 sleep方法
代码如下:
/** * @author gc * @date 2022/11/18 15:05 * note : 1.模拟延时 * 2.倒计时 * 放大问题的发生性 */ public class SleepThread{ public static void main(String[] args) { //tenDown(); Date startTime = new Date(System.currentTimeMillis()); // 获取系统当前时间 while(true){ try { Thread.sleep(1000); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime)); startTime = new Date(System.currentTimeMillis()); // 更新当前时间 } catch (InterruptedException e){ e.printStackTrace(); } } } public static void tenDown() throws InterruptedException { int num = 10; while(true){ Thread.sleep(1000); System.out.println(num--); if (num <= 0){ break; } } } }
2.线程礼让 yield方法
/** * @author gc * @date 2022/11/18 17:50 * note : * 1.线程礼让可能不成功,取决于cpu的调度 * 2.线程礼让会让线程进入就绪状态 */ public class YieldThread implements Runnable{ public static void main(String[] args) { YieldThread yieldThread = new YieldThread(); new Thread(yieldThread,"a").start(); new Thread(yieldThread,"b").start(); } @Override public void run() { System.out.println(Thread.currentThread().getName() + "线程执行开始!"); Thread.yield(); System.out.println(Thread.currentThread().getName() + "线程执行结束!"); } // 执行结果 礼让过后 线程重新进入就绪状态争夺时间片 //a线程执行开始! //b线程执行开始! //b线程执行结束! //a线程执行结束! }
3.线程强制执行 join方法
/** * @author gc * @date 2022/11/20 12:02 * note : 线程启动 主线程与子线程争夺时间片运行 当主线程满足相应条件后 子线程进行join操作后,主线程就会等待 直到子线程执行完毕 */ public class JoinThread 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 { JoinThread joinThread = new JoinThread(); Thread thread = new Thread(joinThread); thread.start(); for (int i = 0; i < 500; i++) { if (200 == i){ thread.join(); } System.out.println("主线程正在执行!" + i); } } }
4. 观测线程状态 ===> Thread.State
/** * @author gc * @date 2022/11/20 12:21 * note : 测试线程状态 */ public class StateThread { public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(()->{ for (int i = 0; i < 5; 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){ Thread.sleep(100); state = thread.getState(); System.out.println(state); } } }
5. 线程的优先级 ===> Priority
/** * @author gc * @date 2022/11/20 17:36 * note : 线程优先级测试 * 优先级知识提高线程被执行的概率 并不是优先级高一定先执行 超过优先级范围会报错 */ public class PriorityThread { 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); t2.start(); t3.setPriority(4); t3.start(); t4.setPriority(Thread.MAX_PRIORITY); t4.start(); } } class MyPriority implements Runnable{ @Override public void run() { // 子线程优先级 System.out.println(Thread.currentThread().getName() + "线程优先级===>" + Thread.currentThread().getPriority()); } }
6. 守护线程 ===> daemon
/** * @author gc * @date 2022/11/21 17:14 * note : 虚拟机不需要等待守护线程执行完毕,必须确保用户线程执行完毕 */ public class DaemonThread{ public static void main(String[] args) { God god = new God(); You you = new You(); Thread thread = new Thread(god); thread.setDaemon(true); // 默认是false 表示是用户线程 ,正常的线程都是用户线程 thread.start(); // 上帝守护线程启动 new Thread(you).start(); // 你用户线程启动 } } // 你 class You implements Runnable{ @Override public void run() { for (int i = 0; i < 36500; i++) { System.out.println("祝你开心快乐生活每一天!"); } System.out.println("==============GoodBye World!=============="); } } // 上帝 class God implements Runnable{ @Override public void run() { while(true){ System.out.println("上帝保佑着你!"); } } }