---
title: "线程的join（）理解"
author: "bjnick"
date: "2011-02-12T11:12:12Z"
tags: []
categories: ["编程语言"]
slug: "join"
draft: false
lastmod: "2011-02-12T11:12:12Z"
id: 904368
url: "https://bjnick.iteye.com/blog/904368"
---

查看了网上的资料，简单来说，join()的理解就是类似方法调用，在正在运行的线程中调用某其他线程，待其完成后继续先前的线程。

      code如下：

```
package com.test.thread;


public class MyjoinTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Thread t=new Thread(new R());
		t.start();
		System.out.println("start...");
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("finished...");
		
	}
	
	

}

class R implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<5;i++){
			System.out.println(i);
		}
			
	}
	
}
```

 

输出如下：

```
start...
0
1
2
3
4
finished...
```

 

如果不join的话，则是：

```
start...
finished...
0
1
2
3
4
```

 

即main线程调用t线程。

over