java多线程快速入门(八)
设置线程优先级:join()
package com.cppdy; class MyThreadA extends Thread{ MyThreadB b; public MyThreadA(MyThreadB b) { this.b=b; } @Override public void run() { try { b.join(); sleep(1000); } catch (Exception e) { } System.out.println("A子线程执行"); } } class MyThreadB extends Thread{ @Override public void run() { try { sleep(1000); } catch (Exception e) { } System.out.println("B子线程执行"); } } public class ThreadDemo4 { public static void main(String[] args) throws Exception{ MyThreadB mt2 = new MyThreadB(); MyThreadA mt1 = new MyThreadA(mt2); mt1.start(); mt2.start(); mt1.join(); System.out.println("主线程执行"); } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步