このブログについて参加、私たちはメソッドのJavaスレッドに参加伝えスレッド同期を実装することで、元のメソッドのマルチスレッド並列実行は、シリアル実行に変換することができます
図コードに示すように、並行して実行されます
public class ThreadTest {
//private static final Long count = 10000L;
public static void main(String[] args){
long base = System.currentTimeMillis();
try {
ThreadJoin t1 = new ThreadJoin("线程1");
ThreadJoin t2 = new ThreadJoin("线程2");
//t1.join();
t1.start();
t1.join();
t2.start();
} catch (Exception e) {
e.printStackTrace();
}
long time = System.currentTimeMillis() - base;
System.out.println("执行时间:"+time);
}
}
class ThreadJoin extends Thread{
private static final Long count = 10L;
public ThreadJoin(String name){
super(name);
}
@Override
public void run() {
//super.run();
for(int i = 1; i <= count; i ++){
System.out.println(this.getName()+":"+i);
}
}
}
情報をプリントアウトし、それがこのようなものです
执行时间:0
线程1:1
线程2:1
线程2:2
线程2:3
线程2:4
线程2:5
线程2:6
线程2:7
线程2:8
线程2:9
线程2:10
线程1:2
线程1:3
线程1:4
线程1:5
线程1:6
线程1:7
线程1:8
线程1:9
线程1:10
シリアル実行を実現するには、シリアル実行されるスレッド2を実行しているスレッド1つの開始の実装が完了した後に達成するための方法を参加追加することができます
public class ThreadTest {
//private static final Long count = 10000L;
public static void main(String[] args){
long base = System.currentTimeMillis();
try {
ThreadJoin t1 = new ThreadJoin("线程1");
ThreadJoin t2 = new ThreadJoin("线程2");
//t1.join();
t1.start();
t1.join();
t2.start();
} catch (Exception e) {
e.printStackTrace();
}
long time = System.currentTimeMillis() - base;
System.out.println("执行时间:"+time);
}
}
class ThreadJoin extends Thread{
private static final Long count = 10L;
public ThreadJoin(String name){
super(name);
}
@Override
public void run() {
//super.run();
for(int i = 1; i <= count; i ++){
System.out.println(this.getName()+":"+i);
}
}
}
线程1:1
线程1:2
线程1:3
线程1:4
线程1:5
线程1:6
线程1:7
线程1:8
线程1:9
线程1:10
执行时间:0
线程2:1
线程2:2
线程2:3
线程2:4
线程2:5
线程2:6
线程2:7
线程2:8
线程2:9
线程2:10
実行の結果を見ると、それはすでにシリアル実行スレッドです
したがって、上記の例では、シーンに転送され、メインスレッドメインの実装前に、スレッド1を完了するために実行しなければならない方法を、参加します
結合方法の役割は、方法Bにスレッド内のスレッドに参加調整する場合、例えば、スレッドの実行は、スレッドを続ける、最初のBを終了し、あります
OK、上記方法は、パラメータを調整せずに参加することであり、また、糸のようなパラメータ、追加することができA.join(10);
、10秒後に実行されるスレッドAは、スレッドBを続行します
注:デフォルトの時間パラメータに参加し、デフォルトは0で、それは(参加です)に参加(0)と同等です。
/**
* Waits for this thread to die.
*
* <p> An invocation of this method behaves in exactly the same
* way as the invocation
*
* <blockquote>
* {@linkplain #join(long) join}{@code (0)}
* </blockquote>
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public final void join() throws InterruptedException {
join(0);
}
ソースコード内のThreadクラスは、デフォルト値0、0を見ることができ、その後、これは、これはどういう意味ですか?0は0の実装ではなく、スレッドの実行を意味するものでは実行スレッドBを続行する前に完了していることではありません
[OK]を、なぜスレッド同期メソッドに参加するための呼び出しを達成することができますか?私たちは、単にコードを見て:
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
//执行时间必须为正数
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
//执行时间为0或者缺省情况
if (millis == 0) {
while (isAlive()) {//表示线程还没执行好
wait(0);//调用线程的wait方法
}
} else {//执行时间大于0的情况
while (isAlive()) {
long delay = millis - now;//循环计算延期时间
if (delay <= 0) {
break;
}
wait(delay);//同样调用线程的wait方法
now = System.currentTimeMillis() - base;
}
}
}
[OK]を、私は実際には、スレッドの同期を達成するためにシーンの待機方法を呼び出す、理解するのはとても簡単で、ソースコードを見て