/*
    线程合并:将指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程,即单线程。
    如在B线程中调用了A的join方法,则线程A执行完后,才会执行线程B。
*/
public class ThreadTest05 
{
    public static void main(String[] args) throws Exception
    {
        Thread t = new Thread(new Processor());
        t.setName("t");
        t.start();

        //合并线程
        t.join();    //t和主线程合并,变成单线程

        //主线程
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+"--->"+i);
        }
    }
}

class Processor implements Runnable
{
    public void run(){
        for(int i=0;i<5;i++){
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){
                
            }
            System.out.println(Thread.currentThread().getName()+"--->"+i);
        }
    }
}

 

相关文章:

  • 2021-09-08
  • 2022-02-02
  • 2022-12-23
  • 2021-12-07
  • 2022-01-08
  • 2022-12-23
  • 2022-02-21
  • 2022-01-23
猜你喜欢
  • 2021-08-07
  • 2022-02-28
  • 2021-09-23
  • 2022-12-23
  • 2021-06-30
  • 2021-06-19
  • 2021-08-22
相关资源
相似解决方案