java线程join_java中线程的插入(调用.join()方法)

[java]代码库package com.demo.thread;

import java.io.IOException;

public class JoinDemo {

//public static class MyRunnable implements Runnable{

//private String threadName;

//private static int count = 0;

//

//

//public MyRunnable(String threadName) {

//this.threadName = threadName;

//System.out.println("第"+ ++count +"次调用MyRunnable的构造函数 创建了"+threadName);

//}

//

//@Override

//public void run() {

TODO Auto-generated method stub

//

//}

//

//}

private static Thread t2 = null;

private static Thread t1 = null;

public static void main(String[] args) throws IOException{

//MyRunnable mr1 = new MyRunnable("FirstThread");

//MyRunnable mr2 = new MyRunnable("SecondThread");

t1 = new Thread(new Runnable() {

@Override

public void run() {

System.out.println("调用进程t1的run()方法");

int i = 0;

while(true){

try {

Thread.sleep(500);

System.out.println("Thread1:"+ ++i);

if(i == 2)

t2.join();

if(i == 6)

t2.join();

if(i == 20)

{

System.out.println("Thread1运行完毕");

break;

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

});

t1.start();

t2 = new Thread(new Runnable() {

@Override

public void run() {

int i = 0;

System.out.println("进入t2的run()方法");

while(true){

try {

Thread.sleep(500);

System.out.println("Thread2:" + ++i);

if(i == 10){

System.out.println("Thread2运行完毕");

break;

}

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

});

t2.start();

}

}

[代码运行效果截图]

pic_58c4b874d012333705.png

[源代码打包下载]

7ab7b94785bbdb79d4aaa6009386543f.gifJoinDemo.rar(50积分)[0 次下载]

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值