Java geek

Related reading:

Java concurrent programming (A) knowledge map

Java concurrent programming atomicity Java concurrent programming visibility Java Concurrent programming Ordering Java Concurrent Programming Creating threads Overview Java Concurrent Programming Introduction (synchronized Java Concurrent Programming (8) Thread Life Cycle (9) Java Concurrent programming (9) Deadlock and deadlock bits Java concurrent programming (10) Java concurrent programming (10) Lock optimization Java Concurrent Programming introduction (11) Stream limiting scenarios and Spring Stream Limiting Scenarios (12) Producer and Consumer patterns – Java concurrent Programming Introduction (13) Read/write locks and cache templates Java Concurrent Programming Introduction (14) CountDownLatch application scenarios CyclicBarrier is an asynchronous task scheduling tool, with the CompleteFeature. You can use the CyclicBarrier to complete tasks. You can use the CyclicBarrier to complete tasks Introduction to Java Concurrent Programming (20) Common Locking Scenarios and Locking Tools Introduction to Java Concurrent Programming (21) The volatile keyword ThreadLocal variable


1. Daemon thread

1. Threads in JAVA are divided into two types, user threads and daemon threads. By default, processes and threads created are user threads

2. When there are user threads in the JVM, the daemon thread does not exit, only when there are no user threads, the daemon thread will exit.

3. The daemon thread created by the user thread will not exit as long as the user thread exists; The child threads created by a daemon thread, regardless of whether the thread is a daemon thread, will exit as the parent thread exits.

2. Application scenario

In producer-consumer mode, both the producer and consumer are threads. When the producer thread exits after determining that it will not generate any more consumption data, the consumer thread does not need to exist. In this case, the consumer thread can be set as a daemon thread.

Note: In a typical Web application, there will be many user threads, and it is not possible for all user threads to exit. There is no point in setting a daemon thread, because you can never exit, and you need to use an additional mechanism to exit.

3. Show me code

2.1 code

import java.util.concurrent.TimeUnit; public class DaemonThreadDemo { private static class ParentThread extends Thread { private boolean childDaemon = false; public ParentThread(boolean childDaemon) { this.childDaemon = childDaemon; } @Override public void run() { ChildThread childThread = new ChildThread(); childThread.setName("daemonChild"); childThread.setDaemon(childDaemon); childThread.start(); System.out.println(getName() + "run...." ); quietlySleep(); } } private static class ChildThread extends Thread { @Override public void run() { while(true) { System.out.println(getName() + " child run...." ); quietlySleep(); } } } private static void quietlySleep() { try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { // nothing to do } } private static void testDependency(boolean threadADaemon, boolean threadBDaemon) { ChildThread childThread1 = new ChildThread(); childThread1.setName("childThread1 "); childThread1.setDaemon(threadADaemon); childThread1.start(); ChildThread childThread2 = new ChildThread(); childThread2.setName("childThread2 "); childThread2.setDaemon(threadBDaemon); childThread2.start(); } private static void testCreateChildThread(boolean parentDaemon, boolean childDaemon) { ParentThread parentThread = new ParentThread(childDaemon); parentThread.setDaemon(parentDaemon); parentThread.start(); } public static void main(String[] args) {// testDependency(true, false); // testDependency(true, false); // testDependency(true, true); // testDependency(true, true); TestCreateChildThread (true,true); testCreateChildThread(true,true); // testCreateChildThread(true,false); TestCreateChildThread (false,true); testCreateChildThread(false,true); TestCreateChildThread (false,false) testCreateChildThread(false,false); }}Copy the code

2.2 Printing Logs

2.2.1 testDependency (true, false);

childThread1  child run....
childThread2  child run....
childThread2  child run....
childThread1  child run....
childThread2  child run....
childThread1  child run....
Copy the code

2.2.2 testDependency (true, true);

childThread1  child run....
Copy the code

2.2.3 testCreateChildThread (true, true);

If no log is generated, exit immediately.

2.2.4 testCreateChildThread (true, false);

If no log is generated, exit immediately.

2.2.5 testCreateChildThread (false, true);

Thread-0run....
daemonChild child run....
daemonChild child run....
Copy the code

2.2.6 testCreateChildThread (false, false);

Thread-0run....
daemonChild child run....
daemonChild child run....
daemonChild child run....
daemonChild child run....
daemonChild child run....
Copy the code

<– read left mark, left point like!