package ye;public class We {
public static void main(String[] args) {
T t = new T();
t.start();
int index=0;
while (true) {
 index++;
if (index==100) 

try {t.join();} catch (Exception e) {e.printStackTrace();}

System.out.println("main:"+index);
}
}}class T extends Thread {
public void run() {
while (true) {

System.out.println("run: "+Thread.currentThread().getName());
}
}}
这段程序在开始时t线程和主线程没有交替执行?
这段程序的输出结果怎么会是:
run: Thread-0
run: Thread-0
run: Thread-0
run: Thread-0
run: Thread-0
...

解决方案 »

  1.   

    join 只是等待 线程终止..等到什么时候.. 谁都不知道.          所以 给 join 加上参数 (1000)或者自己想多少毫秒就是多少毫秒. 就可以了. 加上参数以后  你就能得到你想要的结果了. 当然. 如果你设置成0 .. 超时为 0 意味着要一直等下去。
      

  2.   

    一个线程A在占有cpu资源期间,可以让其他线程调用join和本线程联合,如b.join()则线程A在运行期间联合了b,如果A在占有cpu期间一旦联合线程b,那么线程A将立刻中断执行,一直等到它联合的线程b执行完毕,线程A在重新排队等待cpu资源,以便恢复执行