What thread-related methods are available for 1.object
The object class is a superclass of all Java classes, providing three methods that are closely related to threads, wait (), notify (), Notifyall ()
Wait () provides three overloaded forms, timed waits, and infinite waits (the thread waits until another thread calls the object's notify () or Notifyall () method)
Notify () wakes up a single thread waiting on this object monitor
Notifyall () wakes up all the threads waiting on this object monitor
2. Thread deadlock
RUN1 Implements runnable{
public void Run () {
Synchronized (") {
}
}
}
Get the current thread name: Thread.CurrentThread (). GetName ()
Create threads: Thread th=new Thread (new Run1 ()); Th.start ();
Four conditions of deadlock:
1. Mutex 2. Non-deprivation 3. Request and hold 4. Cyclic wait
3. Which interfaces can return a thread with a return value
Callable in conjunction with the future interface, executors contains a practical method for converting from a common class to a callable class
public interface callable<v>{
V call () throws Exception;
}
public class Transfer implements callable<integer>{
Public Integer call () {
return i;
}
}
Transfer tran=new Transfer ();
Futuretask ft=new futuretask<integer> (Tran);
Thread thr=new thread (ft);
Thr.start ();
4. Which classes can be used to create a thread pool
The thread pool is a collection of threads, the same as the connection pooling function
If you need to create a large number of short-life-cycle threads in your system, you need to use the thread pool
The 1>executor class provides some static methods to get the thread pool, for example, Newcachedthreadpool ()--Create a new thread without a thread, Newfixedthreadpool ()--Creates a fixed-size thread
Using the thread pool, use Executorservice.shutdown ()
The 2>threadgrop class, a thread group class similar to thread pool, that groups each thread to a member of a thread group management
5. Background threads (Daemon threads)
Role: Service for other threads
How to set: Call the Start () method of the thread before the Setdaemon (Boolean on) method
Thread:isdaemon (), Setdaemon (Boolean on)
The system resource recycling mechanism in the JVM is a typical example of a background thread
6. Thread hibernation
The thread's sleep () is a static method that allows the currently running thread to pause execution for a specified time
7. Terminating a thread
1>stop (): Unsafe, no cleanup has been done to the thread before it terminates, and all monitors that have been locked by the threaded object will be freed
2>suspend (): Insecure, with a fixed deadlock tendency, if the target thread hangs on a monitor that protects critical system resources, the target thread restarts before any thread can access the resource, if the thread that started the target thread wants to call resume () Method locks the monitor before a deadlock occurs
3> using a Boolean value to terminate a running thread
4>isinterrupted (), Interrupted () safe termination
8. Thread Suspend and resume
Suspend: Let go of CPU usage rights, temporarily stop running, because time is uncertain, so suspend need wait ()
Recovery: Let the suspended thread resume execution and continue the execution of the thread from the point of interruption. Suspending the thread's object execution Notifyall () method
9. The difference between synchronous and asynchronous
Synchronization: Avoid deadlocks, dirty data, and modify the shared resources in order (phone, waiting for the receiver to return information)
Async: Can improve efficiency, asynchronous processing can work at the same time, but the guarantee must be concurrent processing (broadcast, do not care about the recipient's status, not waiting for the recipient's return information)
Difference: Need to wait and no need to wait
10. Sync unlock to resolve shared resources
Synchronous statement block, synchronous method, also can be synchronously unlocked
Reentrantlock Lock.lock (); Lock.unlock ();
11. When a thread is involved
Typically, multiple requirements are at the same time, or long-awaited operations are required. file, network, IO device
Java Interview Basics accumulate----multi-threaded, concurrent