Thread 线程(三)

来源:互联网 发布:lol for mac 国服 编辑:程序博客网 时间:2024/05/17 09:35

线程等待

package com.dragon.test3;/** * 线程等待 * @author Administrator * */public class Test {

 

 /**  * @param args  */ public static void main(String[] args) {  // TODO Auto-generated method stub  FirstThread firstThread = new FirstThread();  SecondThread secondThread = new SecondThread();  ThirdThread thirdThread= new ThirdThread();  firstThread.start();  secondThread.start();  thirdThread.start(); }

 

}/** * 创建第一个线程 * @author Administrator * */class FirstThread extends Thread{ public void run(){  for (int i = 0; i < 5; i++) {   try {    System.out.println("第一个线程第"+i+"次循环");    Thread.sleep(1000);//线程休眠   } catch (InterruptedException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }  }   }}/** * 第二个线程 * @author Administrator * */class SecondThread extends Thread{ public void run(){  for (int i = 0; i < 5; i++) {   try {        System.out.println("第二个线程第"+i+"次循环");    Thread.sleep(1000);//线程休眠   } catch (InterruptedException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }   if(i==3){    System.out.println("加入线程一");    try {     FirstThread.currentThread().join();    } catch (InterruptedException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }//加入线程一   }     }   }}/** * 第三个线程 * @author Administrator * */class ThirdThread extends Thread{ public void run(){  for (int i = 0; i < 5; i++) {   try {        System.out.println("第三个线程第"+i+"次循环");    Thread.sleep(1000);//线程休眠   } catch (InterruptedException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }  }   }}

 

原创粉丝点击