Interviewer: You know even the multithreading problem, how do you want me to "release the water"?

Interviewer: Let me ask you a few questions about multi-threading. Tell me about the cause of thread deadlock. How to remove thread deadlock?

 

Programmer Ali: This...deadlock...

(In one minute)

Interviewer: Don’t know? Well, what is the difference between Lock and Synchronized?

Programmer Ali: Lock is a lock...

Interviewer: ...will be, if not, say no, save time, should you know the difference between sleep and wait?

Programmer Ali: ...I don't know much

Interviewer: There are several ways to implement multithreading. What is thread safety, should you know?

Programmer Ali: Can we ask another technical point?

Interviewer: You don’t even know how to proceed with multithreading.

Open UC browser to see more wonderful pictures

In Java development, multithreading generally refers to the technology that realizes the common execution of multiple tasks, which is a relatively basic and commonly used technology.

To understand multithreading, you need to understand a few concepts:

 

Process: Every program running on the system is a process. Each process contains one or more threads. The process may also be the dynamic execution of the entire program or part of the program.

Thread: A thread is a set of instructions, or a special section of the program, which can be executed independently in the program. It can also be understood as the context in which the code runs. So threads are basically lightweight processes, which are responsible for performing multiple tasks in a single program. The operating system is usually responsible for the scheduling and execution of multiple threads. A thread is a single sequential control flow in a program.

Multithreading: Run multiple threads at the same time to complete different tasks in a single program, which is called multithreading.

Concurrency: In the operating system, concurrency means that several programs in a period of time are between started and run to completion, and these programs are all running on the same processor, but there are only A program runs on the processor.

Multi-threaded concurrency is also a technical point that is likely to be asked in interview questions.

Below I have compiled a set of core interview points related to multi-threaded concurrency, and summarized all the common interview questions of multi-threaded concurrency.

The maps of these knowledge points and the PDF documents with detailed answers to the questions can be shared with everyone for free. After helping the editor forward the article, private message [multi-threaded] to receive it for free!

 

Java multithreaded concurrency

1. JAVA Concurrent Knowledge Base

2. JAVA thread implementation / creation method

1. Inherit the Thread class

2. Implement the Runnable interface

3. ExecutorService, Callable<Class>, Future have return value threads

4. Thread pool based approach

 

3. 4 kinds of thread pools

1. newCachedThreadPool

2. newFixedThreadPool

3. newScheduledThreadPool

4. newSingleThreadExecutor

 

4. Thread life cycle (state)

1. New state (NEW)

2. Ready state (RUNNABLE)

3. RUNNING

4. Blocked state (BLOCKED)

  • Waiting for blocking (o.wait->waiting queue)

  • Synchronous blocking (lock->lock pool)

  • Other blocking (sleep/join)

5. Thread death (DEAD)

  • Normal end

  • Abnormal end

  • Call stop

5. 4 ways to terminate threads

1. End of normal operation

2. Use the exit flag to exit the thread

3. The Interrupt method ends the thread

4. The stop method terminates the thread (thread is not safe)

6. The difference between sleep and wait

7. The difference between start and run

8. JAVA background thread

9. JAVA lock

1. Optimistic lock

2. Pessimistic lock

3. Spin lock

  • Advantages and disadvantages of spin locks

  • Spin lock time threshold (1.6 introduces adaptive spin lock)

  • The opening of the spin lock

4. Synchronized synchronization lock

  • Synchronized scope

  • Synchronized core components

  • Synchronized implementation

5. ReentrantLock

  • The main method of the Lock interface

  • Unfair lock

  • Fair lock

  • ReentrantLock 与 synchronized

  • ReentrantLock implementation

  • The difference between Condition class and Object class lock method

  • The difference between tryLock and lock and lockInterruptibly

6. Semaphore

  • Implement mutex lock (counter is 1)

  • Code

  • Semaphore 与 ReentrantLock

7. AtomicInteger

8. Reentrant lock (recursive lock)

9. Fair locks and unfair locks

  • Fair lock (Fair)

  • Nonfair lock (Nonfair)

10. ReadWriteLock

  • Read lock

  • Write lock

11. Shared locks and exclusive locks

  • Exclusive lock

  • Shared lock

12. Heavyweight lock (Mutex Lock)

13. Lightweight lock

  • Lock upgrade

14. Bias lock

15. Segmented lock

16. Lock optimization

  • Reduce lock holding time

  • Reduce lock granularity

  • Lock separation

  • Chain coarsening

  • Lock elimination

10. Thread basic method

1. Thread waiting (wait)

2. Thread sleep (sleep)

3. Thread yield (yield)

4. Thread interrupt (interrupt)

5. Join waiting for other threads to terminate

6. Why use join() method?

7. Thread wake up (notify)

8. Other methods

11. Thread context switch

1. Process

2. Context

3. Register

4. Program Counter

5. PCB-"Switch Frame"

6. Context switching activities

7. Causes of thread context switching

12. Synchronization and deadlock

1. Synchronization lock

2. Deadlock

13. Thread Pool Principle

1. Thread reuse

2. The composition of the thread pool

3. Rejection Policy

4. Java thread pool work process

14. JAVA blocking queue principle

1. The main method of blocking the queue

  • Insert operation

  • Get data operation

2. Blocking queues in Java

3. ArrayBlockingQueue (fair, unfair)

4. LinkedBlockingQueue (two independent locks improve concurrency)

5. PriorityBlockingQueue (compareTo sorting implementation priority)

6. DelayQueue (cache invalidation, timing task)

7. SynchronousQueue (does not store data, can be used to transfer data)

8. LinkedTransferQueue

9. LinkedBlockingDeque

15. Usage of CyclicBarrier, CountDownLatch, Semaphore

1. CountDownLatch (thread counter)

2. CyclicBarrier (loop fence-wait until the barrier state and then all execute at the same time)

3. Semaphore (semaphore-control the number of threads simultaneously accessed)

16. The role of the volatile keyword (variable visibility, prohibit reordering)

1. Variable visibility

2. No reordering

3. A more lightweight synchronization lock than sychronized

4. Applicable scenarios

17. How to share data between two threads

1. Abstract the data into a class, and use the data manipulation as a method of this class

2. Runnable object as an inner class of a class

18. ThreadLocal function (thread local storage)

1. ThreadLocalMap (an attribute of thread)

2. Usage scenarios

19. The difference between synchronized and ReentrantLock

1. What they have in common

2. The difference between the two

20. ConcurrentHashMap Concurrency

1. Reduce lock granularity

2. ConcurrentHashMap segment lock

  • ConcurrentHashMap is composed of Segment array structure and HashEntry array structure

21. Thread scheduling used in Java

1. Preemptive scheduling

2. Cooperative scheduling

3. JVM thread scheduling implementation (preemptive scheduling)

4. Threads give up cpu

22. Process scheduling algorithm

1. Priority scheduling algorithm

2. High priority priority scheduling algorithm

3. Round-robin scheduling algorithm based on time slice

23. What is CAS (compare and exchange-optimistic locking mechanism-lock spin)

1. Concepts and characteristics

2. Atomic package java.util.concurrent.atomic (lock spin)

3. ABA problem

24. What is AQS (Abstract Queue Synchronizer)

1. Exclusive Exclusive resource-ReentrantLock

2. Share shared resources-Semaphore/CountDownLatch

3. The realization of the synchronizer is the ABS core (state resource state count)

4. ReentrantReadWriteLock implements exclusive and shared methods

Answer preview:

The maps of these knowledge points and the PDF documents with detailed answers to the questions can be shared with everyone for free. After helping the editor forward the article, private message [666] to receive it for free!

 

Guess you like

Origin blog.csdn.net/bieber007/article/details/108802578