1, Join Introduction:
just learned today, here I briefly summed up that join itself is the meaning of "join", then what is the meaning of the thread? The Join method is used during the execution of the Run method of one of the threads, and when there are some other actions that need to be performed in a particular case.
Assume:
When executing a thread, in the Run method of a, a new thread B is created and started, then there are 2 scenarios:
(1): B does not call the Join method:
At this time A and B threads are actually in the same level of state, a, B threads can be said to be a competitive relationship, the common contention for CPU usage rights.
There is no one that must be executed before, that must be executed after the concept!
(2): B calls the Join method:
This time, the situation is not the same as (1)!
At this point: in the Run method of a, the code block executes to B when the creation and execution of the time, a thread will temporarily stop, let the CPU use permission, this time
The B-thread begins to perform the operation, and after the execution of the B-thread-controlled operation finishes, the a thread continues to perform the next task from the stopped operation!
2, The simple example illustrates:
1 PackageCn.sxt.joins;2 /**3 * Join thread: Join4 * 5 * Simulation: Mother cooking is to let son to buy soy sauce6 * @authorSmall Wind micro-spirit7 *8 */9 Ten One classMextendsthread{ A - Public voidrun () { -System.err.println ("Noon! Come to Dinner! "); theSystem.out.println ("Mother ready--cooking.") "); -System.out.println ("Mother ready--wash the vegetables." "); -System.out.println ("Mother ready--cut the vegetables." "); -System.out.println ("Mother prepares-stir-fry, found no soy sauce, let son to buy soy sauce!" "); + - s s=new s (); S.start (); A Try { at /** Analysis Description: - * The Join method must be called after the child worker thread is started to be effective! - * - * Join () Effect: - * During the current thread execution, temporarily joining a worker thread, at this point, the current thread is paused, - * Continue executing the current thread after the worker thread finishes executing! in */ - s.join (); //The son buys the soy sauce to join the mother to cook the thread to go to}Catch(interruptedexception e) { + e.printstacktrace (); - } the *System.out.println ("Mom started cooking.") "); $System.out.println ("The rice is ready, the family eats together.") ");Panax Notoginseng - } the } + A classSextendsthread{ the Public voidrun () { +System.out.println ("Son went downstairs.") "); - $ Try { $Thread.Sleep (1000); -}Catch(interruptedexception e) { - e.printstacktrace (); the } - WuyiSystem.out.println ("The son arrives super, buys the soy sauce!") "); theSystem.out.println ("The son went upstairs. "); - Wu Try { -Thread.Sleep (1000); About}Catch(interruptedexception e) { $ e.printstacktrace (); - } - -System.out.println ("The son gave the soy sauce to his mother. "); A } + } the - Public classJointhread { $ the /** the * Startup program the * @paramargs the */ - Public Static voidMain (string[] args) { in the NewM (). Start (); the } About the}
Operation Result:
It's noon! Come to Dinner!
Mother prepares--cooking.
Mother prepares--to wash vegetables.
Mother prepares-cut vegetables.
Mother prepared-stir fry, found no soy sauce, let son to buy soy sauce!
The son went downstairs.
The son arrives super, buys the soy sauce!
Son went upstairs.
Java threading learns to organize--2---join another thread