Java multithreading Series 2-thread control, java multithreading 2-Thread

Source: Internet
Author: User

Java multithreading Series 2-thread control, java multithreading 2-Thread

The previous article has introduced thread scheduling. Next I will introduce how to use this method to control threads.

1. Thread sleep

Public static void sleep (long millis)

/** Thread sleep * public static void sleep (long millis) */public class ThreadSleepDemo {public static void main (String [] args) {ThreadSleep ts1 = new ThreadSleep (); threadSleep ts2 = new ThreadSleep (); ThreadSleep ts3 = new ThreadSleep (); ts1.setName ("zhangsan"); ts2.setName ("lisi"); ts3.setName ("wangwu "); ts1.start (); ts2.start (); ts3.start () ;}} public class ThreadSleep extends Thread {@ Override public void run () {for (int x = 0; x <100; x ++) {System. out. println (getName () + ":" + x + ", Date:" + new Date (); // sleep try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace ();}}}}

2. Add threads

Public final void join ()

Public class ThreadJoin extends Thread {@ Override public void run () {for (int x = 0; x <100; x ++) {System. out. println (getName () + ":" + x) ;}}/** public final void join (): waiting for the thread to terminate. */Public class ThreadJoinDemo {public static void main (String [] args) {ThreadJoin tj1 = new ThreadJoin (); ThreadJoin tj2 = new ThreadJoin (); threadJoin tj3 = new ThreadJoin (); tj1.setName ("zhangsan"); tj2.setName ("lisi"); tj3.setName ("wangwu"); tj1.start (); try {tj1.join ();} catch (InterruptedException e) {e. printStackTrace () ;}tj2.start (); tj3.start ();}}

3. Thread courtesy

Public static void yield ()

Public class ThreadYield extends Thread {@ Override public void run () {for (int x = 0; x <100; x ++) {System. out. println (getName () + ":" + x); Thread. yield () ;}}/ ** public static void yield (): Pause the currently running thread object and execute other threads. * It makes the execution of multiple threads more harmonious, but it cannot be used to ensure a single thread at a time. */Public class ThreadYieldDemo {public static void main (String [] args) {ThreadYield ty1 = new ThreadYield (); ThreadYield ty2 = new ThreadYield (); ty1.setName ("zhangsan "); ty2.setName ("lisi"); ty1.start (); ty2.start ();}}

4. Background thread

Public final void setDaemon (boolean on)

Public class ThreadDaemon extends Thread {@ Override public void run () {for (int x = 0; x <100; x ++) {System. out. println (getName () + ":" + x) ;}}/ ** public final void setDaemon (boolean on): Mark this thread as a daemon thread or user thread. * When all running threads are daemon threads, the Java Virtual Machine exits. This method must be called before the thread is started. **/Public class ThreadDaemonDemo {public static void main (String [] args) {ThreadDaemon td1 = new ThreadDaemon (); ThreadDaemon td2 = new ThreadDaemon (); td1.setName ("zhangsan"); td2.setName ("lisi"); // set the harvest Thread td1.setDaemon (true); td2.setDaemon (true); td1.start (); td2.start (); Thread. currentThread (). setName ("wuyudong"); for (int x = 0; x <5; x ++) {System. out. println (Thread. currentThread (). getName () + ":" + x );}}}

5. Thread interruption

Public final void stop ()

Public void interrupt ()

Import java. util. date; public class ThreadStop extends Thread {@ Override public void run () {System. out. println ("start execution:" + new Date (); // I want to rest for 10 seconds try {Thread. sleep (10000);} catch (InterruptedException e) {// e. printStackTrace (); System. out. println ("thread terminated");} System. out. println ("end execution:" + new Date () ;}/ ** public final void stop (): stops the thread. It is out of Date, but can also be used. * Public void interrupt (): interrupt thread. Terminate the thread state and throw an InterruptedException. */Public class ThreadStopDemo {public static void main (String [] args) {ThreadStop ts = new ThreadStop (); ts. start (); // if you do not wake up for more than three seconds, try {Thread. sleep (3000); // ts. stop (); ts. interrupt ();} catch (InterruptedException e) {e. printStackTrace ();}}}

 

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.