preface
Summary of 40 questions
2. How threads are created
Interface oriented programmingIt is also at the heart of the six principles of design patterns.
3. The difference between the start() and run() methods
4. Difference between Runnable interface and Callable interface
5. Difference between CyclicBarrier and CountDownLatch
The role of the volatile keyword
7. What is thread safety
The mechanism of fail – fast 。
How to obtain thread dump file in Java
9. What happens to a thread with a runtime exception
How do I share data between two threads
What’s the difference between sleep and wait
12. What is the role of the producer-consumer model
13. What does ThreadLocal do
Space for time, in each Thread maintains a method implemented with open address ThreadLocal. ThreadLocalMap, isolating data, data is not Shared, nature is no Thread safety issues
14. Why wait() and notify()/notifyAll() are called in synchronous blocks
15. What is the difference between wait() and notify()/notifyAll() when giving up object monitors
17, how to check if a thread has object monitor
“Thread” refers to the current thread 。
The difference between synchronized and ReentrantLock
19. What is the concurrency of ConcurrentHashMap
20. What is ReadWriteLock
Read locks are shared, while write locks are exclusiveTo improve read and write performance, read and write are mutually exclusive instead of mutually exclusive.
21. What is FutureTask
How do I find which thread uses the longest CPU in Linux
Java programming to write a program that will cause a deadlock
24, How to wake up a blocked thread
26. What is multithreaded context switching
27. What happens if the thread pool queue is full when you submit a task
28. What is the thread scheduling algorithm used in Java
What does thread.sleep (0) do
30. What is spin
What is the Java memory model
Main memory and working memory. Class status, which is Shared between classes of variables, are stored in main memory, each Java thread used variables in the main memory, will read a variables in main memory, and make those within their working memory has a copy, running his own thread code, use these variables, operation is the a in the working memory. After the threaded code completes execution, the latest values are updated to main memory
What is the optimistic lock and pessimistic lock
Compare – SettingsThese two actions attempt to modify a variable in memory as an atomic operation, and failure indicates a conflict, so there should be retry logic.
34, What is AQS
Singleton thread safety
An instance of a class can only be created once in a multithreaded environment. There are several ways to write the singleton pattern, so LET me summarize:
Limit the number of concurrent requests for a block of code. Semaphore has a constructor that passes in an integer of type n to indicate that a piece of code can be accessed by at most n threads. If more than n is passed, wait until one thread finishes executing the block before the next thread enters. If the Semaphore constructor passes an int n=1, it becomes synchronized.
37, Hashtable size() = “return count”;
38, Thread class constructor, static block is called by which thread
Synchronization method and synchronization block, which is the better choice
Lock coarseningThis method is to make the synchronization range larger. This is useful, for example, StringBuffer, which is a thread-safe class. Naturally, the most common append() method is a synchronous method, and when we write code we repeatedly append the string, which means we repeatedly lock -> unlock, which is bad for performance, Because this means that the Java virtual machine is repeatedly switching between kernel and user mode on this thread, the Java virtual machine coarses the code for multiple Append calls into a lock, extending the multiple Append operations to the top and bottom of the Append method into a large synchronized block. This reduces the number of locks -> unlocks, effectively increasing the efficiency of code execution.
40. How can thread pools be used for high concurrency and short task execution times? How can thread pools be used for businesses with low concurrency and long task execution times? How can a business with high concurrency and long business execution time use thread pools?
For more technical information: Gzitcast