Java Control thread and java Control thread

Source: Internet
Author: User

Java Control thread and java Control thread

1. join

public class JoinThreadTest extends Thread {        public JoinThreadTest(String name){        super(name);    }        @Override    public void run() {        for(int i = 0; i < 100; i++){            System.out.println(getName() + " " + i);        }    }        public static void main(String[] args) {        for(int i = 0; i < 1000; i++){            if (i== 20) {                JoinThreadTest joinThreadTest = new JoinThreadTest("JoinThread");                joinThreadTest.start();                try {                    joinThreadTest.join();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }                        System.out.println(Thread.currentThread().getName() + " " + i);        }    }}

After the join method is called, the main thread is blocked until the join thread added by the join () method is executed. It is usually used to convert a large problem into a small problem. Each small problem is assigned a thread. When the threads of these small problems are executed, the main thread is called for further processing.

2. Background thread Daemon

The main thread is the foreground thread by default, and the foreground thread is the foreground thread by default, and the background thread is the background thread by default. When all foreground threads die, background threads automatically die. Set the thread to a background thread. When setDaemon (true) is called, it must be called before start () to determine whether a thread is a background thread and can be determined by isDaemon.

public class DaemonThreadTest extends Thread{        public DaemonThreadTest(String name){        super(name);    }        @Override    public void run() {        for (int i = 0; i < 1000; i++) {            System.out.println(getName() + " " + i);        }    }        public static void main(String[] args) {        DaemonThreadTest daemonThreadTest = new DaemonThreadTest("DaemonThread");        daemonThreadTest.setDaemon(true);        daemonThreadTest.start();        for (int i = 0; i < 100; i++) {            System.out.println(Thread.currentThread().getName() + " " + i);        }    }}

After running the code above, we can see that DaemonThread will not output to 999, but when the main line is finished, after a period of time, DaemonThread will end.

3. sleep and yield

After sleep is called, the thread enters the blocking state. During the sleep time of the thread, the thread will not receive execution opportunities even if the cpu is idle. After sleep, the thread enters the ready state, waiting for scheduling to enter the running state. Valid for all threads with priority.

Yield simply suspends the current thread and re-schedules the thread scheduler. It is only valid for threads with a priority greater than or equal to the current thread. The current thread may also be scheduled to run again after being paused.

In addition, calling sleep throws InterruptException, but yield does not.

public class YieldTest extends Thread {        public YieldTest(String name) {        super(name);    }        @Override    public void run() {        for (int i = 0; i < 100; i++) {            System.out.println(getName() + " " + i);            if (i== 20) {                Thread.yield();            }        }    }        public static void main(String[] args) {        YieldTest yieldTest1 = new YieldTest("High");        yieldTest1.setPriority(MAX_PRIORITY);        yieldTest1.start();                YieldTest yieldTest2 = new YieldTest("Low");        yieldTest2.setPriority(MIN_PRIORITY);        yieldTest2.start();    }}

Because the above Code sets different priorities, execute the above Code. When the execution reaches I = 20, although the High thread gives up the resource, but the priority is High, after rescheduling, or the High thread continues to execute, comment out the two lines of code that set the priority and execute the code. After I = 20, the thread is scheduled, and the High thread will send the resource to Low for running.

4. Change the thread priority.

In the preceding example, the priority of a subthread is the same as that of the parent thread by default.

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.