java进阶 线程池 join用法总结:thread4.join();方法,就表明thread4.join();这个线程受到贵客待遇,直到这个线程执行完,被插入这个方法的载体线程才可以执行。

那个线程调用join

举例

thread4.join();方法,就表明thread4.join();这个线程受到贵客待遇,直到这个线程执行完,被插入这个方法的载体线程才可以执行。



package javajinjie.char29.threadpool;



public class TaskThreadDemo 
{
public static void main(String[] args)
{
//Create tasks
Runnable printA = new PrintChar('a', 100);
Runnable printB = new PrintChar('b', 100);
Runnable print100 = new PrintNum(100);

//Create threads
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(print100);

//Run threads
thread1.start();
thread2.start();
thread3.start();
}


}
class PrintChar implements Runnable
{
//The task for printing a character a specified number of times
private char charToPrint; //The character to print
private int times;// The number of times to repeat

//Construct a task with specified character and number of times to print the character
public PrintChar(char c, int t)
{
charToPrint = c;
times = t;
}


public void run()
{
for (int i = 0; i < times; i++)
System.out.print(charToPrint);
System.out.println();
}
}




class PrintNum implements Runnable
{
private int lastNum;



public PrintNum(int n)
{
lastNum = n;
}


public void run()
{
Thread thread4 = new Thread(new PrintChar('c',400));
thread4.start();
try
{
for (int i = 1; i <= lastNum; i++)
{
System.out.print(" " + i);
if(i==50)
{
System.out.print("----------look here ");
thread4.join();
}
}

}
catch(Exception e)
{
System.out.print(e);
}
}
}




/**result:
aaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50----------look here cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
*/

你可能感兴趣的:(java进阶 线程池 join用法总结:thread4.join();方法,就表明thread4.join();这个线程受到贵客待遇,直到这个线程执行完,被插入这个方法的载体线程才可以执行。)