Java Collection - Efficient Concurrent Threads

Concurrent programming is a basic part of Java, and it is also a common question in interviews


1. What is the difference between a process and a thread?
Answer: A process is the smallest resource allocation unit of a computer. To put it bluntly, it is a program that can run independently. A thread is the smallest unit of execution in a computer, created by a process.
Difference:
Processes can run independently and own resources; threads must rely on processes to run, and threads themselves do not own resources unless programmers use local threads to allocate resources.
Threads are created by processes, and a process can create multiple threads; when a process dies, all threads are buried with it; when a thread dies, the process is not affected.
Process switching involves CPU and resource allocation issues, while switching between threads only involves registers.
2. What is the relationship between multithreading and single threading?
Answer: Multi-threading refers to a process that has more than 1 threads, each of which is an independent execution body compared to other threads, and the switching between threads has context switching in a single-core CPU. The method is time slice rotation; for a single thread, there is no context (environment) switching and no time slice rotation.
Multithreading is generally managed by a thread pool to save system resources.
3. What are the states of the thread?
Answer: Seven, new, runable, running, block, wait, timed wait (suspended), terminated.
4. What are the thread activity faults? Explain separately.
Answer: deadlock, livelock, deadlock, starvation

  • Deadlock: Resource demand loop. (unalienable, mutual wait, resource mutual exclusion, request hold)
  • Livelock: Can't ask for it (request a certain resource but can't get it all the time)
  • Locked: refers to the thread has been unable to wake up. There are two reasons: nested lock and semaphore loss
  • Hunger: I want to complete a certain task, but I have resources and cannot request it.
    5. What are the safety features of thread and its explanation?
    Answer: There are three security features of threads:
  • Atomicity: The operation of the transaction inside the thread is invisible to other threads.
  • Visibility: The operations of threads on shared resources can be fed back to other threads.
  • Orderedness: In-memory operations appear ordered to other processors.
    6. Talk about your understanding of the synchronized keyword.
    Answer: Synchronized is a heavyweight lock provided by Java. Its function is to lock key resources and prevent other threads from modifying them. Its underlying implementation is a counter. Whenever the modified member is in the unaccessed state, the bottom timer value does not change; if it is in the accessed state, first change the bottom counter, and then perform corresponding operations.
    7. Talk about your understanding of the volatile keyword.
    Answer: volatile is a lightweight lock that can guarantee the visibility and order of resources, but does not guarantee atomicity. For example, when a resource is changed, other threads can access this resource, but this access can only be read, not written.
    8. The difference between ReentrantLock and synchronized:
    Answer: ReentrantLock is a kind of reentrant lock, and its lock needs to be applied and released manually, which ensures the further strengthening of resource control, and at the same time avoids the occurrence of deadlock to a certain extent (depending on API). Synchronized is a heavyweight lock. The locked member will only release the lock after execution, allowing other objects to access (depending on the JVM).
    9. Do you know the thread pool in Java?
    Answer: Yes, the thread pool can be thought of as a pool full of threads, mainly to solve the situation of resources wasted by frequent creation and destruction of threads. The thread pool must have the following three characteristics, namely maximum capacity, minimum capacity, and core capacity.

Guess you like

Origin blog.csdn.net/qq_44503987/article/details/112726765