1. Sometimes, a thread is also called a lightweight process. Like a process, a threadProgramIs an independent and concurrent execution path.
A thread has its own stack, its own program counters, and its own local variables. However, compared with the separated process
The degree of isolation between threads is small. They share memory, file handles, and the status that each other process should have.
2. Each Java program has at least one thread-main thread. When a Java program is started, JVM will create the main thread and
Call the main () method of the program in this thread.
3. Reasons for using threads:
· Faster UI response
· Using a multi-processor system
· Simplified modeling
· Asynchronous or background processing
· Execute blocking I/O without blocking the entire program
4. In some cases, using a thread can simplify programming and maintenance. For example, if you want to simulate an application
Simulate interaction between multiple entities. Giving each entity a thread of its own makes many simulations and modeling applications large.
Simplified.
5. Although threads can greatly simplify many types of applications, excessive use of threads may compromise program performance and maintainability.
The thread consumes resources. Therefore, the number of threads that can be created is limited without compromising performance.
In a single processor system, using multiple threads does not make the program that consumes CPU resources faster.
6. If the object has a thread, it should provide a start () or Init () method to start the thread, instead
Start it from the constructor.
7. The thread ends in one of the following three ways:
· The thread reaches the end of its run () method.
· The thread throws an uncaptured exception or error.
· Another thread calls an obsolete stop () method. Abandon means that these methods still exist, but you should not
CodeAnd try to remove them from existing code.
8. If the thread is interrupted by calling thread. Interrupt (), the sleeping thread will throw
Interruptedexception, so that the thread will know that it is awakened by the interruption, so it does not have to check whether the timer has expired.
9. The thread. Yield () method is like thread. Sleep (), but it does not cause sleep. Instead, it just suspends the current thread for a moment so that other threads can run.
10. The garbage collection thread and other threads created by JVM are all daemon threads.
Any thread can become a daemon thread. You can call the thread. setdaemon () method to indicate that a thread is
Program thread. You may want to use the daemon thread as the background thread created in the program, such as the timer thread or other delayed event thread, only when other non-daemon threads are running, these threads are useful.
11. Volatile is simpler than synchronized. It is only suitable for controlling access to a single instance of basic variables (such as integers and Boolean variables.
12. Because the block is protected by a lock, it does not mean that two threads cannot execute the block at the same time. It only indicates that if two threads
Waiting for the same lock, they cannot execute the code at the same time.
13. Because synchronization prevents multiple threads from executing a code block at the same time, performance is problematic, even on a single processor system.
It is best to use synchronization on the smallest possible block of code to be protected.
14. Volatile means synchronization.
15. Several guidelines for compiling synchronized blocks:
* Keep the code block short.
* Do not block. Do not call methods that may cause blocking in synchronized blocks or methods, such
Inputstream. Read ().
* Do Not Call methods for other objects when holding locks. This may sound extreme, but it eliminates the most common
Deadlock source.
16. java. util. Concurrent is a Java-dedicated concurrent package.