Java multithreading --- Thread. join Introduction
I haven't written a blog for a long time, and I have never had time to go to work before. But I always feel that writing a blog is a good way to record my technical process.
So after I resigned, I came to beisoft, went to school again, and began to write a blog again. So I started to write a blog again.
Great !!!!
This is the join method of Thread. In the past, the run and sleep methods were always used. Which two of them are clear? For this join method, his
The main function is to use a thread similar to thread1.join () when you call other threads in a method.
It starts to wait until the thread1 thread returns. When will the thread1 thread's run method return after running?
Continue to run. Of course, there are two parameter methods for join. This parameter means to wait for the thread to finish calling, such
Thread1.sleep (1000), so that the main thread will wait for thread1 to run until thread1 finishes running and returns, or the current main thread will not wait for more than 1 second
Thread 1 continues to run down. The Code is as follows:
Package com. bird. concursey; import java. util. date; import java. util. concurrent. timeUnit;/*** data source * @ author bird * September 15, 2014 10:10:04 */public class DataSourceLoader implements Runnable {public void run () {System. out. println ("begining the data source loding" + new Date (); try {TimeUnit. SECONDS. sleep (4);} catch (InterruptedException e) {e. printStackTrace ();} System. out. println ("begining the data source loding" + new Date ();} public static void main (String [] args) {celceloader dsLoader = new DataSourceLoader (); thread thread1 = new Thread (dsLoader, "dsloader"); NetworkConnectionsLoader ncLoader = new NetworkConnectionsLoader (); Thread thread2 = new Thread (ncLoader, "ncloader"); thread1.start (); thread2.start (); try {thread1.join (); // thread2.join (1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} System. out. println (new Date ());}}
package com.bird.concursey;import java.util.Date;import java.util.concurrent.TimeUnit;public class NetworkConnectionsLoader implements Runnable {public void run() {System.out.println("begining the data source loding" + new Date());try {TimeUnit.SECONDS.sleep(6);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("begining the data source loding" + new Date());}}
begining the data source lodingMon Sep 15 22:27:41 CST 2014begining the data source lodingMon Sep 15 22:27:41 CST 2014begining the data source lodingMon Sep 15 22:27:45 CST 2014Mon Sep 15 22:27:46 CST 2014begining the data source lodingMon Sep 15 22:27:47 CST 2014
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + CjxwPjxpbWcgc3JjPQ = "" alt = "">