I have compiled all Java-related interview questions and answers into PDF, and bookmarked the table of contents, which is very convenient to read
questions and answers PDF download : https://www.hicxy.com/2645.html
questions and answers PDF download : https://www.hicxy.com/2645.html
questions and answers PDF download : https://www.hicxy.com/2645.html
1. What is the difference and connection between multi-threaded and single-threaded?
1, In a single-core CPU, the CPU is divided into small time slices, and only one thread can be executed at a time, which is a microscopic mechanism that takes turns occupying the CPU.
2, multithreading will have thread context switching, which will slow down the execution speed of the program, that is, the time required to execute a process with two threads is longer than the time required to execute a process with one thread twice.
Conclusion: That is, the use of multithreading will not increase the execution speed of the program, but will reduce the speed, but for the user, it can reduce the user's response time.
2. What is context switching in multithreading?
Multiple threads will use the CPUs on a group of computers together, and when the number of threads is greater than the number of CPUs allocated to the program, in order to allow each thread to have the opportunity to execute, it is necessary to use the CPU in rotation. Different thread switching uses the switching data generated by the CPU, etc., which is context switching.
3. What is the role of join method?
The main function of the join method in the Thread class is synchronization, which can make parallel execution between threads become serial execution. When we call this method of a thread, this method will suspend the calling thread, and the calling thread will not continue execution until the called thread finishes its execution.
4. What are the functions of creating thread pool parameters?
public ThreadPoolExecutor( int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
1.corePoolSize: The size of the core thread pool. When a task is submitted, the thread pool will create a thread to perform the task. Even if other idle core threads can perform new tasks, it will be created. The number of tasks waiting to be executed is greater than the core size of the thread. Will not continue to create.
2.maximumPoolSize: the maximum number of thread pools, the maximum number of threads allowed to be created, if the queue is full and the number of threads created is less than the maximum number of threads, a new thread will be created to perform tasks. If it is an unbounded queue, this parameter is basically useless.
3.keepAliveTime: The thread keeps alive time, the time that the thread pool worker thread remains alive after being idle, so if there are many tasks and the execution time of each task is short, the time can be adjusted to increase the thread utilization.
4.unit: The time unit of thread keep alive, days (DAYS), hours (HOURS), minutes (MINUTES, milliseconds MILLISECONDS), microseconds (MICROSECONDS), nanoseconds (NANOSECONDS)
5.workQueue: task queue, a blocking queue that holds tasks waiting to be executed.
Generally speaking, the following blocking queues can be selected:
ArrayBlockingQueue: Bounded blocking queue based on array.
LinkedBlockingQueue: Blocking queue based on linked list.
SynchronizedQueue: A blocking queue that does not store elements.
PriorityBlockingQueue: A blocking queue with priority.
6.threadFactory: Set the factory for creating threads. You can set a more meaningful name for each created thread through the thread factory.
7.handler: The saturation strategy is also called the rejection strategy. When the queue and thread pool are full, it reaches saturation. So we need to adopt strategies to deal with new tasks. The default policy is AbortPolicy.
AbortPolicy:直接抛出异常。
CallerRunsPolicy: 调用者所在的线程来运行任务。
DiscardOldestPolicy:丢弃队列里最近的一个任务,并执行当前任务。
DiscardPolicy:不处理,直接丢掉。
当然可以根据自己的应用场景,实现RejectedExecutionHandler接口自定义策略。
5. Give examples of synchronous and asynchronous.
If there are critical resources in the system (the number of resources is less than the number of threads competing for resources), for example, the data being written may be read by another thread in the future, or the data being read may have been written by another thread. These data must be accessed synchronously (exclusive locks in database operations are the best example). When an application calls a method on an object that takes a long time to execute, and does not want the program to wait for the method to return, it should use asynchronous programming. In many cases, it is more efficient to use asynchronous methods. In fact, the so-called synchronization refers to blocking operations, while asynchronous refers to non-blocking operations.
For the rest of the problem, you can first think about it for yourself.
In addition, I have sorted out all Java-related interview questions and answers for your reference
questions and answers PDF download : https://www.hicxy.com/2645.html
questions and answers PDF download : https://www.hicxy.com/2645.html
questions and answers PDF download : https://www.hicxy.com/2645.html
6. How to create a thread pool
7. Why wait() method and notify()/notifyAll() method should be called in synchronized block
8. Why should the wait and notify methods be called in a synchronized block?
9. Implementation principle of join method
10. What is the difference between notify() and notifyAll()?
11. Why wait, notify and notifyAll are not in the thread class?
12. # 2. Synchronize static methods
13. After a thread enters a synchronized instance method of an object, can other threads enter other methods of this object?
14. What is your understanding of thread priority?
15. Close the thread pool
16. There are several ways to achieve thread synchronization and mutual exclusion, what are they?
17. What is a Daemon thread? What's the point of it?
18. What is synchronized lock?
19. What is the difference between CycliBarriar and CountdownLatch in Java?
20. What is the difference between wait and sleep methods?
21. Why are the sleep() and yield() methods of the Thread class static?
22. What is the thread scheduling algorithm used in Java?
23. There are three threads T1, T2, and T3. How do you ensure that T2 is executed after T1 is executed, and T3 is executed after T2 is executed?
24. What is the difference between Executor, ExecutorService, and Executors in Java?
25. The understanding of optimistic lock and pessimistic lock and how to realize it, what are the ways to realize it?
26. How to stop a running thread
27. What is ThreadLocal
28. What is the Java memory model
29. What is an immutable object, and how does it help to write concurrent applications?
30. What is an atomic operation? What are the atomic classes in the Java Concurrency API?
31. What is the concurrency of ConcurrentHashMap
32. Tell me how I use the synchronized keyword. Have you used it in your project? There are three main ways to use the synchronized keyword:
33. What is Semaphore in Java?
34. How to share data between two threads?
35. What is ThreadLocal variable?
36. How to detect whether a thread owns a lock?
37. How do you get the thread stack in Java?
38. How to set the thread pool reasonably
39. How many uses of Synchronized?
40. What is the difference between sleep method and wait method
41. What is a thread pool? Why use it?
42. What happens if an exception occurs when a thread is running?
43. How to control the size of a method that allows concurrent access threads?
44. What is the difference between user thread and daemon thread?
45. Can a thread interrupt call stop directly? Why?
46. The difference between Runnable interface and Callable interface
47. What is the use of ThreadLocal
48. What is the difference between heap and stack in Java?
49. How to get thread dump file in Java
50. What is Thread Scheduler and Time Slicing?
51. Do not use stop to stop the thread?
52. What is the difference between SynchronizedMap and ConcurrentHashMap?
53. How to use thread pools for businesses with high concurrency and short task execution time? How do businesses with low concurrency and long task execution time use thread pools? How do businesses with high concurrency and long business execution time use thread pools?
54. How do you ensure that the thread where the main() method is located is the thread where the Java program ends last?
55. Which is a better choice, synchronization method or synchronization block?
56. Thread pool role
57. What application scenarios can CopyOnWriteArrayList be used for?
58. How to create a daemon thread? And when to use it?
59. How to share data between two threads
60. The difference between synchronized and ReentrantLock
61. What is the role of Thread.sleep(0)
62. What is the difference between thread sleep() method and yield() method?
63. What is a blocking queue? What is the realization principle of blocking queue? How to use blocking queues to implement the producer-consumer model?
64. What is the difference between a daemon thread and a local thread in java?
65. What is the Executors framework?
66. What is the Java Timer class? How to create a task with a specific time interval?
67. How to pause the running thread for a period of time?
68. What is spin
69. How do you wake up a blocked thread in Java?
70. Deadlock in Java
71. The difference between start() method and run() method
72. Join and start call sequence problem
73. What is the Lock interface in Java Concurrency API? What are its advantages over synchronization?
74. What is a thread pool? Why use it?
75. Thread creation method
76. Tell me about my understanding of the synchronized keyword
77. Why use the Executor framework?
78. Thread Status
79. Briefly describe the basic concepts of threads, programs, and processes. And what is the relationship between them?
80. # 5, synchronization object instance
81. # 1. Synchronization common method
82. The main members of the Executor framework
83. Which two important jvm instructions does synchronized include?
84. What is the function of the yield method in the Thread class?
85. Thread class construction method, static block is called by which thread
86. What is FutureTask
87. The role of the volatile keyword
88. What is thread safety?
89. What is the difference between volatile variables and atomic variables?
90. Why wait(), notify() and notifyAll () must be called in a synchronized method or synchronized block?
91. How to ensure thread safety?
92. What is the realization of concurrent containers?
93. # 3. Synchronization
94. How to avoid deadlock and detection
95. Why is it better to use the Executor framework than to use the application to create and manage threads?
96. What is the difference between wait() method and notify()/notifyAll() method when giving up the object monitor
97. Tell me about the underlying principle of the synchronized keyword
98. What is the difference between interrupted and isInterrupted methods in Java?
99. What is a reentrant lock (ReentrantLock)?
100. Thread pool workflow
101. What is the difference between livelock and deadlock in Java?
102. The state transition of the thread?
103.
104. What are Callable and Future?
105. What are the basic states of threads?
106. Why do we execute the run() method when we call the start() method, and why can’t we call the run() method directly?
107. Please tell me the methods related to thread synchronization and thread scheduling.
108. What is blocking (Blocking) and non-blocking (Non-Blocking)?
109. What is a blocking method?
110. What is the difference between synchronous collection and concurrent collection in Java?
111. What is a race condition? How do you discover and resolve competition?
112. Common thread pool modes and usage scenarios of different thread pools?
113. The difference between deadlock and livelock, and the difference between deadlock and hunger?
114. Atomic operations update field classes in Java. Which classes are provided by the Atomic package?
115. Submit tasks to the thread pool
116. What is the difference between pass and not pass in join method?
117. # 4, synchronize this instance
118. How do you deal with uncontrollable exceptions in threads?
119. In multithreading, what is context-switching (context-switching)?
120. What is the use of multithreading?
121. Synchronization method and synchronization block, which is the better choice
122. Why does the code reorder?
123. What is the difference between interrupted and isInterruptedd methods in Java?
124. What is thread safety
125. What is the difference between parallel and concurrency?
126. What are the advantages and disadvantages of spin locks?
127. What is the difference between the submit() and execute() methods in the Java thread pool?
128. What happens if a thread has a runtime exception
129. What is volatile? Can order be guaranteed?
130. ThreadLocal principle, points of attention when using, and what are the application scenarios?
131. The level of thread safety
132. What is the difference between notify and notifyAll in Java?
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。