The first thread has both a daemon thread and a user thread, the difference being whether the user thread keeps the program running. When the program is running, there must be one or more threads that are user threads, and when the program finishes, all the daemon threads will be closed. Using Thread.setdaemon (ture), you can mark a thread as a daemon thread, and the default thread state inherits from the thread that created it. Two ways to create a thread are not much to say.
Thread safety generally refers to the inconsistent data or data integrity of shared variables that are accessed and read by multiple threads. Generally there are several methods to be consulted:
The 1.synchronized method provides a class, method, or block of statements that can be accessed only by one thread, and the modification of the control variable is within the controllable range.
The 2.lock method can be controlled using a read-write or a re-entry lock within the Java.util.concurrent.locks package.
3.ThreadLocal variable to get a copy of a variable for each thread separately.
4. Atomic operations at the machine instruction level, such as variables in the Java.util.concurrent.atomic package. Use CAs (compare and swap) to ensure the validity of data updates. That is, the lock-free mechanism to ensure high concurrency performance, maintain high throughput.
5. I'm not sure yet.
The JDK itself provides a thread pool implementation that is convenient to use.
There are so many thread pools in general
Cond
Threads and Thread Pools