Ali's interviewer shared the technical points of multi-threaded concurrency that need to be prepared for Java interviews

Questions about multithreading and concurrency are an essential part of any Java interview. So you should prepare a lot of questions about multithreading. Multithreading and concurrency is a very popular topic in investment banking, especially as it relates to the development of electronic transactions. They ask interviewers a lot of confusing Java threading questions. The interviewer just wants to make sure that the interviewee has sufficient knowledge of Java threads and concurrency, since many of the candidates are only superficial. High-volume and low-latency electronic trading systems for direct-to-market transactions are inherently concurrent.

1. Concept

what is thread

  • For a thread to perform a task, there must be a thread

  • All tasks of a process (program) are executed in threads

  • A thread executes tasks serially, that is to say, a thread can only execute one task at a time.

The principle of multithreading

  • At the same time, the CPU can only process 1 thread, and only one thread is working (executing)

  • Multi-threaded concurrent (simultaneous) execution, the essence of which is that the CPU quickly schedules (switches) between multiple threads

What if there are too many threads?

  • The CPU is scheduled in N multiple threads, which consumes a lot of CPU resources

  • The lower the frequency that each thread is scheduled to be executed (the execution efficiency of the thread is low)

Advantages of Multithreading

  • Can appropriately improve the execution efficiency of the program

  • Can properly improve resource utilization (CPU memory utilization, etc.)

Disadvantages of Multithreading

  • There is an overhead in creating threads. The main costs under iOS include: kernel data structure (about 1KB), stack space (sub-thread 512KB, main thread 1MB, you can also use -setStackSize: to set, but it must be a multiple of 4K, and the minimum is 16K), it takes about 90ms to create a thread

  • If you open a large number of threads, it will reduce the performance of the program

  • The more CPUs the program has, the greater the overhead on threads

  • Programming is more complex: communication between threads, data sharing among multiple threads

The main role of the main thread

  • Display and refresh the UI interface

  • Handling UI events (such as click events, scroll events, drag events, etc.)

Note on the use of the main thread

  • Don't put time-consuming operations on the main thread, it will cause the UI interface to freeze

  • Put time-consuming operations on child threads (background threads, not main threads)

Two, 4 schemes of multi-threading

Ali's interviewer shared the technical points of multi-threaded concurrency that need to be prepared for Java interviews

Three, common multi-threaded interview questions:

Below are some Java thread questions that I like to ask at different times and places. I don't provide an answer, but I'll give you clues whenever possible, and sometimes those clues are enough to answer the question.

1. Now 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?

This threading question is usually asked during the first round or phone interview to test your familiarity with the "join" method. This multi-threading problem is relatively simple and can be implemented with the join method.

2. What is the advantage of Lock interface over synchronized block in Java? You need to implement an efficient cache that allows multiple users to read, but only one user to write, in order to maintain its integrity, how would you implement it?

The biggest advantage of lock interfaces in multi-threaded and concurrent programming is that they provide locks for reading and writing separately, which can satisfy you when writing high-performance data structures like ConcurrentHashMap and conditional blocking. Java thread interview questions are increasingly being asked based on the interviewee's answers. I highly recommend reading Locks carefully before you go for a multithreaded interview, as it currently has a lot of client cache and transaction connection space used to build electronic transaction systems.

3. What is the difference between wait and sleep methods in java?

Java threading interview questions that are often asked during phone interviews. The biggest difference is that wait releases the lock while waiting, while sleep keeps holding the lock. Wait is usually used for inter-thread interaction, and sleep is usually used to suspend execution.

4. Write code in Java to solve the producer-consumer problem.

Similar to the question above, but this one is more classic, and is sometimes asked in interviews. How to solve the producer-consumer problem in Java, of course there are many solutions, I have shared a method implemented by blocking queue. Sometimes they even ask how to implement the Philosopher's meal.

5. How will you use thread dump? How would you analyze a Thread dump?

In UNIX you can use kill -3 and thread dump will print the log, in windows you can use "CTRL+Break". Very simple and professional thread interview question, but tricky if he asks you how to analyze it.

6. Programming a program in Java that will cause a deadlock, how would you solve it?

This is my favorite Java threading interview question because even though deadlock problems are very common when writing multithreaded concurrent programs, many candidates can't write deadlock free code (deadlock free code?) and they struggle. Just tell them that you have N resources and N threads, and you need all of them to complete an operation. For simplicity, n can be replaced by 2, larger data will make the problem look more complicated. Learn more about deadlocks by Avoiding Deadlocks in Java.

7. What are the common problems you encounter in a multithreaded environment? How did you solve it?

Commonly encountered in multi-threaded and concurrent programs are Memory-interface, race conditions, deadlocks, livelocks, and starvation. The problems are endless, and if you get it wrong, it will be hard to spot and debug. This is mostly interview based, not practical application based Java threading questions.

8. Why does the run() method execute when we call the start() method, and why can't we call the run() method directly?

This is a very classic java multithreading interview question. This is also the confusion when I first started writing thread programs. Now this question is usually asked in phone interviews or in the first round of junior-intermediate Java interviews. The answer to this question should be this, when you call the start() method you will create a new thread and execute the code in the run() method. But if you call the run() method directly, it will not create a new thread nor execute the calling thread's code.

9. What is the function of the volatile key in Java? How to use it? How is it different from the synchronized method in Java?

Since Java 5 and Java memory model changes, threading issues based on the volatile keyword have become more and more popular. Be prepared to answer questions about how volatile variables ensure visibility, ordering, and consistency in a concurrent environment.

10. What is an immutable object and how does it help write concurrent applications?

Another classic multi-threaded interview question, not directly related to threads, but indirectly helpful a lot. This java interview question can get really tricky if he asks you to write an immutable object, or asks you why String is immutable.

The relevant part of the interview guidance has been completed. If you have a friend who is preparing for an interview, but does not have a thorough understanding of multi-threaded concurrency, you can join the group: 697579751. I recorded some interviews that are often encountered in multi-threaded concurrency. Difficulties and difficulties, and also Videos such as the underlying principles of multithreading. Those who are interested can join in. Finally, a very comprehensive multi-threaded concurrent learning roadmap is provided.

Four, multi-threaded concurrent learning ideas:

Ali's interviewer shared the technical points of multi-threaded concurrency that need to be prepared for Java interviews

The most comprehensive concurrent programming architecture system in history

The above picture is the most comprehensive multi-threaded concurrent learning knowledge points I have summarized since I was engaged in Java development. The content looks a lot and is very complicated. In fact, it is very easy to learn. I will often upload some information about distributed learning in my group. Architecture, microservice architecture, source code analysis, JVM, and concurrent programming learning videos are shared with friends in the group and each topic section will have a learning video diagram. If you are interested, you can add my group: 697579751 to learn for free .

V. Summary

In fact, many interviewers are aware that junior programmers have almost zero mastery of multithreading. However, in the interview session, many interviewers will ask one or two multi-threaded questions with a try attitude. Its purpose is not to make it difficult for the interviewer, nor to show the "tallness" of the interview, but to distinguish between ordinary talents and high-quality talents. One last thing to keep in mind :

When looking for a job, in addition to doing some relevant interviews and preparing to familiarize yourself with previous projects, you should also learn new knowledge. After all, the more you learn, the better your chances of successfully landing a job. Moreover, in the IT industry, technology is developing too fast. If there is no motivation to learn new knowledge, it will eventually be eliminated.

Because learning is really boring. Learning is a long-term investment, and it is difficult to see benefits in the short term. Therefore, it is best to set a goal for yourself in learning this, and learn a little every day, and you will reach it slowly. If you're not motivated, set yourself a reward. This can stimulate interest in learning. Another point is that things that can be done today should never be left to tomorrow. After all, people are lazy. You have to believe in yourself, what you can do today will not be done tomorrow .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324897466&siteId=291194637