1. There are several ways to implement a thread in Java?
There are two ways to implement multithreading, namely inheriting the thread class and implementing the Runnable interface.
The difference between the two methods is that if your class has inherited other classes, then you can only choose to implement the Runnable interface, because Java only allows single-inheritance.
2. How do I stop a running thread?
(1) Method 1: Call the Thread.stop () method to stop the thread.
Thread.stop () is unsafe and is no longer recommended for use.
This method is inherently unsafe. Use thread.stop () to stop a thread, resulting in the release (unlocking) of all monitors that the thread has locked (unlocked due to unchecked exception Threaddeath that propagate upward along the stack). If any objects previously protected by these monitors are in an inconsistent state, the inconsistent state of the object (the corrupted object) is visible to other threads, which can result in arbitrary behavior.
(2) method 2:thread.interrupt ()
- Pass interruptedexception. This is the most sensible strategy to pass an exception to the caller of the method.
- Resume interrupts. In cases where it cannot be thrown, such as the Runnable method, you must capture interruptedexception and restore the interrupt state through the interrupt () method of the current thread, so that the higher-level code in the call stack will see an interrupt thrown.
(3) Method 3: Stopping a thread best practice is an interrupt + condition variable.
Interrupts are just a collaborative mechanism that requires interrupted threads to handle interrupts themselves.
The best way to stop a thread is to take advantage of shared conditional variables.
3. What is the difference between notify () and Notifyall ()?
void Notify (): Wakes up a thread that is waiting for the object.
void Notifyall (): Wakes all threads that are waiting on the object.
4. What is the difference between sleep () and wait ()?
Sleep is a threading class (thread) method that causes this thread to pause execution for a specified time, giving the execution opportunity to other threads, but the monitoring state remains and is automatically restored when it is finished. Calling sleep does not release the object lock.
Wait is a method of the object class that calls the wait method on this object to cause this thread to discard the object lock, enter the waiting lock pool waiting for this object, and only after the Notify method (or Notifyall) is issued for this object the thread enters the object lock pool ready to get the object lock into the running state.
5. What is a daemon thread? What is the point of it?
The so-called background (daemon) thread is a thread that provides a generic service in the background while the program is running, and this thread is not an integral part of the program. Therefore, when all non-background threads are introduced, the program terminates and kills all background threads in the process. Conversely, the program will not terminate as long as any non-background threads are still running. The Setdaemon () method must be called before a thread is started before it can be set as a background thread. Note: The background process terminates its run () method without executing the finally clause.
6. How does Java enable communication and collaboration between multiple threads?
Java provides 3 very important ways to skillfully solve communication problems between threads. These 3 methods are: Wait (), notify (), and Notifyall (). They are all the final methods of the object class, so each class has them by default. Although all classes have these 3 methods by default, they are only meaningful if the Synchronized keyword is scoped to the same synchronization problem and is used in conjunction with the 3 methods. The syntax format that these methods declare in the object class is as follows:
Final void throws interruptedexception Final void Notify () Final void Notifyall ()
Java Face question-multithreading