1. Join Thread :
During thread execution, you sometimes want to have another thread execute first, such as splitting a big problem into many small problems, assigning threads to each small problem, but after all the minor problems have been processed, let the main thread further operate. At this point we can call the join () method of the other thread in the main thread to block the calling thread (here for the main thread).
Sample code:
Copy Code code as follows:
Package org.frzh.thread;
public class Jointhread extends thread{
Provides a parameter constructor that is used to set the name of a thread
Public Jointhread (String name) {
Super (name);
}
public void Run () {
for (int i = 0; i < i++) {
System.out.println (GetName () + "" + i);
}
}
public static void Main (string[] args) {
Start Child threads
The new Jointhread (the "Fresh Thread"). Start ();
for (int i = 0; i < i++) {
if (i = = 20) {
Jointhread JT = new Jointhread ("threaded by join");
Jt.start ();
The main thread calls the JT thread join method, the main thread must wait for JT to perform
try {
Jt.join ();
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
System.out.println (Thread.CurrentThread (). GetName () + "" +i);
}
}
}
There were three sets of threads (two child threads and one main thread), and after i=20, the main thread was blocked and had to wait until the "join thread" had finished executing, so only two threads were executed thereafter.
Three overloaded forms of the Join () method:
Join (): Wait for the join thread to finish;
Join (Long Millis): Waits to be executed by the join thread for a maximum of Mills Hao seconds, after which the join thread will not wait until it has been executed;
Join (long millis, int nanos): Waits for the join thread to execute for a maximum time of millis milliseconds +nanos microseconds. (This method is basically not available).
2: Background thread :
There is a thread that he is running in the background, and his task is to serve other threads, which are called "Background threads", "Daemon Threads" or "Wizard threads." When all foreground threads are dead, the background thread automatically dies.
Sample code:
Copy Code code as follows:
Package org.frzh.thread;
public class Daemonthread extends thread{
public void Run () {
for (int i = 0; i < 1000; i++) {
System.out.println (GetName () + "" +i);
}
}
public static void Main (string[] args) {
Daemonthread dt = new Daemonthread ();
Set this thread as a background thread
Dt.setdaemon (TRUE);
Dt.start ();
for (int i = 0; i < i++) {
System.out.println (Thread.CurrentThread (). GetName () + "" + i);
}
The foreground thread ends, then the background thread DT also ends, so it executes less than 999
}
}
The main thread defaults to the foreground thread, and the child thread created by the foreground thread defaults to the foreground thread, and the child thread created by the background thread defaults to the background thread.
3. Thread Sleep:
The previous join method is to have the calling thread wait for the join thread to finish executing before continuing, and the sleep () method is to have the calling thread block for a period of time before it is ready to be scheduled again. It is therefore usually used to suspend execution of the program.
Sample code:
Copy Code code as follows:
Package org.frzh.thread;
Import Java.util.Date;
public class sleepthread{
public static void Main (string[] args) {
for (int i = 0; i < i++) {
System.out.println ("Current time:" + new Date ());
try {
Thread.Sleep (1000);
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
Two ways to overload the Sleep () method:
static void sleep (Long Millis): Lets the current thread pause Millis milliseconds and enter a blocking state. This method is affected by the accuracy and alignment of the system timer and the thread scheduler.
static void sleep (long millis, int Nanos): Pauses mills milliseconds +nanos microseconds and enters a blocking state, which is also affected by the accuracy and alignment of the system timer and the thread scheduler. Basically not.
4. thread Concession (yield):
The yield () method is similar to the sleep method, and it can also suspend the currently running thread, but he will not block the thread, just put him in the ready state (note that it is not a blocking state). The yield () method only gives the line that is equal to its priority or higher priority Chengyu the chance of execution, so a thread can then be dispatched back to execution after calling the method.
Sample code:
Copy Code code as follows:
Package org.frzh.thread;
public class Yieldthread extends thread{
Public Yieldthread () {
}
Public Yieldthread (String name) {
Super (name);
}
public void Run () {
for (int i = 0; i < i++) {
System.out.println (GetName () + "" +i);
if (i = = 20) {
Current thread Concessions
Thread.yield ();
}
}
}
public static void Main (string[] args) {
Start two concurrent threads
Yieldthread yt1 = new Yieldthread ("Advanced");
Set YT1 as top priority
Yt1.setpriority (thread.max_priority);
Yt1.start ();
Yieldthread yt2 = new Yieldthread ("low level");
Yt2.setpriority (thread.min_priority);
Yt2.start ();
/*
* If the thread is not prioritized, the two threads have the same priority, so the two threads execute alternately, and the other thread executes when the yield is invoked;
* However, after setting the above priority to two threads, the advanced thread execution is just beginning, and when i=20, the yield is invoked, but because the yield method will only
* Give it the same priority or higher priority thread execution opportunity, so this is still an advanced thread execution, not a low-level thread
*/
}
}
5: Change the priority of the thread :
This is simpler, simply by invoking the Invoke instance method setpriority (int priority) method. Each thread defaults to the same priority as its parent thread, and the main thread defaults to a normal priority (5). Java provides 1~10 priority, or you can use three static constants:
Max_priority:10
Min_priority:1
Norm_priority:5
Note that although Java provides 10 priority levels, different systems support the same priority, so try to avoid using the numbers between 1~10 directly, and use static constants to ensure good portability.