Java Basics Multi-threading--Control threads

Source: Internet
Author: User
Tags thread class

Join thread

Invoking the Join () method of another thread in one thread causes the current thread to enter a blocking state until the join thread finishes executing. The Join method is similar to wait, which usually calls the join method of other threads in the main thread, which ensures that some uniform steps are completed in the main thread after all the child threads have been executed.
Here's an example,

Package Threads;public class Jointhread extends Thread {public jointhread (String name) {super (name);} public void Run () {for (int i=0; i<20; i++) {System.out.println (GetName () + "" +i);}} public static void Main (string[] args) throws interruptedexception {new Jointhread ("New Thread-1"). Start (); for (int i=0; i <50; i++) {if (i==20) {Jointhread JT = new Jointhread ("New Thread-2"), Jt.start (),//main thread called the Join method of the JT thread, main thread// You must wait for JT to execute Jt.join ();} System.out.println (Thread.CurrentThread (). GetName () + "" +i);}}

Execution results, visible when the main thread i=20, the main thread is blocked, the test thread-1 and thread-2 are executing, until the end of thread-2 execution, the main line friend continue to execute

Main 0new Thread-1 0main 1new Thread-1 1main 2new Thread-1 2main 3new Thread-1 3main 4new Thread-1 4main 5new Thread-1 5ma In 6new Thread-1 6main 7new Thread-1 7main 8new Thread-1 8main 9new Thread-1 9main 10new Thread-1 10main 11new Thread-1 11 Main 12new Thread-1 12main 13new Thread-1 13main 14new Thread-1 14main 15new Thread-1 15main 16new Thread-1 16main 17new T  Hread-1 17main 18new Thread-1 18main 19new Thread-1 19new thread-2 0new thread-2 1new thread-2 2new thread-2 3new thread-2 4new thread-2 5new thread-2 6new thread-2 7new thread-2 8new thread-2 9new thread-2 10new thread-2 11new thread-2 12new t Hread-2 13new thread-2 14new thread-2 15new thread-2 16new thread-2 17new thread-2 18new thread-2 19main 20main 21main 22m  Ain 23main 24main 25main 26main 27main 28main 29main 30main 31main 32main 33main 34main 35main 36main 37main 38main 39main 40main 41main 42main 43main 44main 45main 46main 47main 48main 49
Background thread: Setdaemon

A feature of a background thread is that if all foreground threads are dead, the background thread will die automatically. The JVM's garbage collector is a typical background thread. Thread provides a Isdaemon method to determine if it is a background thread.

The following is an example of the note that must be set before the thread start (Setdaemon (True)) for the background threads.

Package Threads;public class Daemonthread extends Thread {public void run () {for (int i=0; i<1000; i++) {System.out.prin TLN (GetName () + "" +i);}} public static void Main (string[] args) {Daemonthread t = new daemonthread ();//must be set to Background thread T.setdaemon (true) before start; T.start (); for (int i=0; i<3; i++) {System.out.println (Thread.CurrentThread (). GetName () + "" +i);} The main process ends when it runs here, and the background process ends with it}}

Execution results, you can see that the sub-thread I does not accumulate to 1000 end, because the child thread is set to the background thread, the main thread is the end of the child thread, the child thread will end up

Main 0thread-5 0main 1thread-5 1main 2thread-5 2thread-5 3thread-5 4...thread-5 199thread-5 200thread-5 201Thread-5 202
Thread Sleeping: Sleep

Sleep number A static method of the thread class that allows the current thread to enter a blocking state.

Before the time specified by sleep is reached, the thread does not get an execution opportunity, and the sleep method is often used to pause the thread, even if there are available CPU execution slices.

The sleep method needs to throw an exception.

Here's an example,

Package Threads;import Java.util.date;public class Sleepthread {public static void main (string[] args) throws interrupted Exception {for (int i=0; i<10; i++) {System.out.println ("Time:" + new Date ()); Thread.Sleep (1000);}}

Execution results,

time:wed Nov 12:00:58 CST 2016time:wed Nov 12:00:59 CST 2016time:wed Nov 12:01:00 CST 2016time:wed Nov 16 12:01:0 1 CST 2016time:wed Nov 12:01:02 CST 2016time:wed Nov 12:01:03 CST 2016time:wed Nov 12:01:04 CST 2016time:wed Nov 12:01:05 CST 2016time:wed Nov 12:01:06 CST 2016time:wed Nov 12:01:07 CST 2016
Thread priority and threading concession: yield

Priority level

JAVA uses setpriority to change thread priority. The JVM provides three constants that can be used on different platforms, namely Max_priority, Min_priority, norm_priority, and of course 1. Numbers like 10, but different platforms define different numbers, so it is recommended to use constants.

Yield

Yield is also a static method of the thread class, it is similar to sleep, it can pause the current thread, the difference is that it will not let the thread into the blocking state, but directly into the ready state, so that the current thread re-enter the resource competition, At this point, only other threads with a priority equal to or higher than the current thread can get CPU resource execution, otherwise the yield thread will regain the resource to continue execution.

Here's an example,

Package Threads;public class Yieldthread extends Thread{public yieldthread (String name) {super (name);} public void Run () {for (int i = 0; i<50; i++) {System.out.println (GetName () + "" +i), if (i = =) {Thread.yield ();}}} public static void Main (string[] args) throws Interruptedexception {Yieldthread yt1 = new Yieldthread ("Thread-1"); Yt1.set Priority (thread.max_priority);//yt1.setpriority (7); Yt1.start (); Thread.Sleep (1); Yieldthread yt2 = new Yieldthread ("Thead-2  "); yt2.setpriority (thread.min_priority);//yt2.setpriority (1); Yt2.start ();}}

Execution results, you can see, because the thread-1 priority is higher than thread-2, so no wheel is thread-1 call yield or thread-2 call yield, are thread-1 first grab the resources to continue execution

Thread-1 0thread-1 1thread-1 2thread-1 3thread-1 4thread-1 5thread-1 6thread-1 7thread-1 8thread-1 9thread-1 10thread-1 11 Thread-1 12thread-1 13thread-1 14thread-1 15thread-1 16thread-1 17thread-1 18thread-1 19thread-1 20thread-1Thread-1 22thread-1 23thead-2 0thread-1 24thead-2 1thread-1 25thead-2 2thread-1 26thead-2 3thread-1 27thead-2 4t Hread-1 28thead-2 5thread-1 29thead-2 6thread-1 30thead-2 7thread-1 31thead-2 8thread-1 32thead-2 9thread-1 33th   Ead-2 10thread-1 34thead-2 11thread-1 35thead-2 12thread-1 36thead-2 13thread-1 37thead-2 14thread-1 38thead-2 15thread-1 39thead-2 16thread-1 40thead-2 17thread-1 41thead-2 18thread-1 42thead-2 19thread-1 43thead-2 20thread-1Thead-2 21thead-2 22thread-1 45thead-2 23thread-1 46thead-2 24thead-2 25thread-1 47thead-2 26thread-1 48thead-   2 27thread-1 49thead-2 28thead-2 29thead-2 30thead-2 31thead-2 32thead-2 33thead-2 34thead-2 35thead-2 36thead-2 37thead-2 38thead-2 39thead-2 40thead-2 41thead-2 42thead-2 43thead-2 44thead-2 45thead-2 4 6thead-2 47thead-2 48thead-2 49

  

  

Java Basics Multi-threading--Control threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.