设计要求:
- public static void main(String[] args) throws InterruptedException {
- t1TME t1TME = new t1TME();
- Thread thread = new Thread(t1TME);
- for (int i = 1; i <= 10; i++) {
- System.out.println("hi\t" + i + "次" + Thread.currentThread().getName());
- Thread.sleep(1000);
- if (i == 5) {//主线程输出了五次 hi
- thread.start();//启动子线程 输出 hello..
- thread.join();//立即将t1TME插入到主线程
- }
- }
- System.out.println("主线程结束...");
- }
- }
-
- class t1TME implements Runnable {
- int i = 0;
-
- @Override
- public void run() {
- while (true) {
- System.out.println("hello\t" + (++i) + "次" + Thread.currentThread().getName());
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (i == 5) {
- System.out.println("子线程结束...");
- break;
- }
- }
- }
- }