The technical points that need to be prepared for multi-threaded concurrency in Java interviews shared by Ali interviewer

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?

这是我最喜欢的Java线程面试问题,因为即使死锁问题在写多线程并发程序时非常普遍,但是很多侯选者并不能写deadlock free code(无死锁代码?),他们很挣扎。只要告诉他们,你有N个资源和N个线程,并且你需要所有的资源来完成一个操作。为了简单这里的n可以替换为2,越大的数据会使问题看起来更复杂。通过避免Java中的死锁来得到关于死锁的更多信息。

7.你在多线程环境中遇到的共同的问题是什么?你是怎么解决它的?

多线程和并发程序中常遇到的有Memory-interface、竞争条件、死锁、活锁和饥饿。问题是没有止境的,如果你弄错了,将很难发现和调试。这是大多数基于面试的,而不是基于实际应用的Java线程问题。

8.为什么我们调用start()方法时会执行run()方法,为什么我们不能直接调用run()方法?

这是一个非常经典的java多线程面试问题。这也是我刚开始写线程程序时候的困惑。现在这个问题通常在电话面试或者是在初中级Java面试的第一轮被问到。这个问题的回答应该是这样的,当你调用start()方法时你将创建新的线程,并且执行在run()方法里的代码。但是如果你直接调用run()方法,它不会创建新的线程也不会执行调用线程的代码。

9.Java中的volatile关键是什么作用?怎样使用它?在Java中它跟synchronized方法有什么不同?

自从Java 5和Java内存模型改变以后,基于volatile关键字的线程问题越来越流行。应该准备好回答关于volatile变量怎样在并发环境中确保可见性、顺序性和一致性。

10.什么是不可变对象,它对写并发应用有什么帮助?

另一个多线程经典面试问题,并不直接跟线程有关,但间接帮助很多。这个java面试问题可以变的非常棘手,如果他要求你写一个不可变对象,或者问你为什么String是不可变的。

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: 725219329. I recorded some interviews for the difficulties and difficulties that are often encountered in multi-threaded concurrency, 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: 725219329 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=324781775&siteId=291194637