黑马程序员技术交流社区

标题: 自己写的小小的thread的代码,希望能帮助大家 [打印本页]

作者: wome789    时间: 2015-10-25 13:27
标题: 自己写的小小的thread的代码,希望能帮助大家
写的是join的使用。
  1. package Thread;

  2. public class TestJoin {
  3.         public static void main(String[] args) {
  4.                 MyThread2 t1 = new MyThread2("T1");
  5.                 t1.start();
  6.                 try{
  7.                         t1.join();
  8.                 }
  9.                 catch(InterruptedException e){}
  10.                
  11.                 for(int i=1;i<=10;i++) {
  12.                         System.out.println("i am main thread");
  13.                 }
  14.         }
  15. }

  16. class MyThread2 extends Thread {
  17.         MyThread2(String s) {
  18.                 super(s);
  19.         }
  20.         public void run() {
  21.                 for(int i=1;i<=10;i++) {
  22.                         System.out.println("i am "+getName());
  23.                         try {
  24.                                 sleep(1000);
  25.                         }
  26.                         catch(InterruptedException e) {
  27.                                 return;
  28.                         }
  29.                 }
  30.         }
  31. }
复制代码








欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2