设置线程优先级: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("主线程执行"); } }
原文地址:https://www.cnblogs.com/cppdy/p/10015479.html
时间: 2024-10-07 13:44:08