首页 > 编程语言 >Java线程的join方法

Java线程的join方法

时间:2022-09-20 16:45:29浏览次数:63  
标签:Java am running 线程 join main public

java线程中的join方法

线程的join方法可以用来让本线程插队,强行占用cpu执行权;
现有线程A在cpu上运行,另一个线程B调用自己的join方法,强行把正在运行的线程A退回到等待状态,线程B自己进入cpu运行。

  1. 示例代码:
package com.cxf.multithread.join;
 
public class TestForJoin implements Runnable{
    
public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new TestForJoin());
        thread.start();
        
        for (int i = 0; i < 6; i++) {
            if(i==2){
                thread.join();  //插队
            }
            System.out.println("main is running");
        }

    }
 
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("i am running");
        }
    }
}
  1. 程序输出:
main is running
main is running

i am running
i am running
i am running
i am running
i am running

main is running
main is running
main is running
main is running

标签:Java,am,running,线程,join,main,public
From: https://www.cnblogs.com/JaxYoun/p/16711580.html

相关文章