1. Wait/notify and Condition.await/condition.signal/condition.signalall
A condition is associated with a lock, like a conditional queue associated with a built-in lock. To create a condition, you can increase the Lock.newcondition method on the associated lock. Just as lock provides a richer feature than built-in locks, condition also provides richer functionality than built-in conditional queues: There can be multiple waits on each lock, conditional waiting can be interruptible or interruptible, time-based waiting, and fair or unfair queue operations. Unlike the built-in conditional queues, you can have any number of condition objects for each lock. The condition object inherits the fairness of the associated lock object, and for a fair lock, the thread is released from the condition.await in FIFO order.
Note: In the Condition object, the Wait,notify and Notifyall methods are Await,signal,signalall respectively. However, condition extends the object, so it also contains the wait and notify methods. Be sure to use the version--await and signal.
The role of condition is to control the lock more precisely. The await () method in condition is equivalent to the wait () method of object, and the signal () method in condition corresponds to the Notify () method of object, condition in Signalall () The Notifyall () method equivalent to object. The difference is, wait () in object, notify (), the Notifyall () method is bundled with the Sync Lock (synchronized keyword), and condition needs to be bundled with a mutex/shared lock.
2. How to avoid deadlock
Deadlock refers to two or more than two processes in the implementation process, as a result of contention for resources caused by a mutual wait for the phenomenon, without external forces, they will not be able to push down. This is a serious problem, because deadlocks will cause your program to suspend the task, the deadlock must meet the following four conditions:
Mutex: A resource can be used by only one process at a time.
Request and retention conditions: a process that is blocked by requesting resources, retains the resources that have been acquired.
Non-deprivation of conditions: the resources that the process has acquired cannot be forcibly deprived until the end of use.
Loop wait Condition: A circular wait resource relationship is formed between several processes.
The easiest way to avoid deadlocks is to block the loop waiting condition, set all the resources in the system to a flag bit, sort, and specify that all process request resources must operate in a certain order (ascending or descending) to avoid deadlocks.
3. There are three threads t1,t2,t3, how to ensure that they are executed sequentially
There are several ways to have threads execute in a specific order in multiple threads, and you can use the join () method of the thread class to start another thread in one thread and another thread to complete the thread to execute. To ensure the order of three threads you should first start the last one (T3 call t2,t2 call T1), so that T1 will complete first and T3 finally complete.
If a thread a executes the thread.join () statement, the meaning is that the current thread a waits for the thread thread to terminate before returning from Thread.Join (). The difference between join and synchronized is that the join uses the Wait () method internally for waiting, while the SYNCHRONIZED keyword uses object monitor as synchronization.
Join provides two other implementations: join (Long Millis) and join (long millis, int nanos), wait at most, exit wait (Release lock), and continue to run after exiting the wait. The interior is achieved by the wait method.
4. If you submit a task, the thread pool queue is full. What will happen when the hair is born?
This is a tricky question, and many programmers will assume that the task will block until there is an empty thread pool queue. In fact, if a task cannot be scheduled to execute then Threadpoolexecutor ' s Submit () method throws a Rejectedexecutionexception exception.
5. What is the difference between the submit () and execute () methods in the Java thread pool
Two methods can submit tasks to the thread pool, the return type of the Execute () method is void, defined in the executor interface, and the Submit () method can return the future object that holds the result of the calculation, defined in the Executorservice interface. It expands the executor interface, and other thread pool classes like Threadpoolexecutor and Scheduledthreadpoolexecutor have these methods.
6. How to create a immutable object in Java.
This problem does not seem to have anything to do with multithreading, but invariance helps to simplify already complex concurrent programs. The immutable object can be shared without synchronization, reducing the synchronization overhead of concurrent access to the object. However, Java does not @immutable this specifier, to create immutable classes, to implement the following steps: To initialize all members by constructing methods, not to provide setter methods for variables, to declare all members private, so that you do not allow direct access to these members, in the Getter method , instead of directly returning the object itself, clones the object and returns a copy of the object.
7. Yield Method
The purpose of the yield () method is to discard the current CPU resources and cede it to other tasks to occupy CPU execution time. But give up time is uncertain, may just give up, and immediately get CPU time slices. Note here that the yield () method is the same as the sleep method, and the thread does not yield a lock, unlike wait.
8. Daemon Threads
There are two kinds of threads in Java, one is a user thread and the other is a daemon thread. When no non-daemon threads exist in the process, the daemon is automatically destroyed. Set the thread to be a background thread by Setdaemon (true). Note that Thread.setdaemon (true) must be in thread. Set before Start (), otherwise the illegalthreadstateexception exception is reported, and the new thread that is generated in the daemon thread is daemon, and the daemon thread is converted to a user thread when using a multiple-threading framework such as Executorserice , and will also set the priority to thread.norm_priority. When building a daemon thread, you cannot rely on the contents of a finally block to ensure that the logic of closing or scavenging resources is performed.
9. Thread-Safe non-blocking queues
Non-blocking queues have concurrentlinkedqueue, Concurrentlinkeddeque. The element cannot be null. Take Concurrentlinkedqueue as an example, there are heads head and tail tail two pointers, follow the principle of FIFO to the team and out, the method has add (e e), peek () removed, poll () removed, remove (Object o), Size (), contains (Objecto), AddAll (Collection c), IsEmpty (). Concurrentlinkeddeque is a two-way queue that can be operated in two directions of the head and tail.
10. Blocking Queues
Arrayblockingqueue: A bounded blocking queue consisting of an array structure.
Linkedeblockingqueue: A bounded blocking queue consisting of a linked list structure.
Priorityblockingqueue: An unbounded blocking queue that supports priority sorting
Delayqueue: An unbounded blocking queue implemented using a priority queue.
Synchronousqueue: A blocking queue that does not store elements.
LinkedTransferQueue: An unbounded blocking queue consisting of a list structure.
Linkedblockingdeque: A two-way blocking queue consisting of a linked list structure.
See Connection: http://www.cnblogs.com/dolphin0520/p/3932906.html
Happens-before rules.
Program Order rule: if operation A in the program before Operation B, then a operation in the thread will precede the B operation.
Monitor lock rule: A unlock operation is currently occurring after the lock operation facing the same lock.
Volatile variable rule: The write operation on a volatile variable first occurs after the read operation of the variable, where the "back" refers to the sequence of time.
Thread Start rule: the Start () method of the Thread object occurs first in every action of this thread.
Thread termination rule: All operations of a thread occur first in the termination detection of this thread, and we can detect that the thread has terminated execution by the Thread.Join () method end, the return value of thread.isalive () equals the segment.
Thread Break rule: The call to the thread interrupt () method first occurs when the code of the interrupted thread detects that the interrupt event occurred.
Finalizer rule: The constructor of an object must be completed before the finalizer of the object is started.
Transitivity: If operation a first occurs in operation B, Operation B first occurs in Operation C, then it can be concluded that operation a first occurs in Operation C.
12. How to avoid deadlock
Deadlock refers to two or more than two processes in the implementation process, as a result of contention for resources caused by a mutual wait for the phenomenon, if there is no external force, they will not be able to push down. This is a serious problem because deadlocks can cause your program to hang up and fail to complete the task, and the deadlock must meet 4 conditions:
Mutex: A resource can be used by only one process at a time.
Request and retention conditions: a process that is blocked by requesting resources, retains the resources that have been acquired.
Non-deprivation of conditions: the resources that the process has acquired cannot be forcibly deprived until it has been used.
Loop wait Condition: A circular wait resource relationship is formed between several processes.
The easiest way to avoid deadlocks is to block the loop waiting condition, set all the resources in the system to flag bits, sort, and specify that all process request resources must be operated in a certain order to avoid deadlocks.
1. How to exchange information between multithreading
2. How to stop a thread
3. What is thread safety, how to guarantee
4. The difference between sleep and wait