Like most first-tier manufacturers interviewer asked 15 multi-threaded Java interview questions

Foreword

Multithreading and concurrency issues are an essential part of any Java interview them. If you want to get more jobs, then you should prepare a lot of questions about multithreading.

They will ask the interviewer a lot of Java threading issues confusing. The interviewer just wanted to make sure the interviewer has enough knowledge of Java threads and concurrency, because there are a lot of candidates just floating on the surface. Now refers to Java5 and contracting issues regarding concurrency tools and concurrent collections are on the rise. Those problems ThreadLocal, Blocking Queue, Counting Semaphore ConcurrentHashMap and more popular.

v2-1d4cfa73fe08a5bf316cbaea7a42d472_hd.webp



Multi-threaded Java interview questions and answers


1. There are T1, T2, T3 three threads, how do you ensure T2 after the implementation of execution in T1, T3 after the implementation of execution in T2?

This question is usually asked thread in the first round or the phone interview stage, the purpose is to detect whether you are familiar with the "join" method. This multi-threaded problem is relatively simple, it can be achieved using the join method.


2. Lock the interface in Java synchronized block than what the advantages are? You need to implement an efficient caching, which allows multiple users to read, but only allows a user to write, in order to maintain its integrity, what would you do to achieve it?

lock interface multithreading and concurrent programming biggest advantage is that they provide a lock to read and write, respectively, it can meet you write high-performance data structures and obstruction conditional like ConcurrentHashMap. Java threads interview questions will be more and more based on answers to questions of the interviewer. I strongly recommend carefully read Locks, since it is currently a large number of customers for building the electronic trading system of the end-side caching and transaction connected space before you go to multi-threaded interview.


3. Different wait and sleep in java methods?

Usually often asked in a phone interview Java thread interview questions. The biggest difference is wait while waiting to release the lock, and sleep has been holding the lock. Wait typically used for inter-thread interactions, sleep is often used to pause execution.


4. implemented in Java blocking queue.

This is a relatively tough interview questions multithreaded, it can achieve many purposes. First, it can detect whether a candidate who can actually write programs in Java threads; second, can detect the candidates' understanding of concurrency scenarios, and you can ask a lot of questions based on this. If he is to achieve blocking queue with a wait () and notify () method, you can ask him to use the latest in Java 5 concurrent class to write again.


5. write code in Java to solve the producer - consumer issues.

Very similar to the above problem, but the problem is more classic, there are times when the interview will ask the following questions. How to solve in Java producer - consumer problem, of course, there are many solutions, I have to share a method implemented by blocking queue. Sometimes they'll even ask how to achieve the Dining Philosophers problem.


6. Java programming will lead to a deadlock in the program, you will be how to solve?

This is my favorite interview questions Java threads, deadlocks because even when writing multi-threaded programs is very common, but many candidates who can not write deadlock free code (no deadlock Code?), They are struggling. Just tell them that you have the resources and the N N threads, and you need all the resources to complete an operation. For simplicity here, n is 2 can be replaced, the larger the data will look more complicated problem. By avoiding deadlocks in Java to get more information about the deadlock.


7. What is atomic, atomic operations in what is Java?

Very simple java interview questions thread, the next question is that you need to synchronize an atomic operation.


volatile key 8.Java in what role? How to use it? In Java it with synchronized method What is the difference?

Since Java 5 and Java memory model changes, threading issues volatile keyword based more and more popular. We should be ready to answer questions about how to ensure that volatile variable visibility, order and consistency in a concurrent environment.


9. What is a race condition? How do you identify and resolve the competition?

This is a multi-threaded appear in the advanced stages of the interview questions. Most interviewers will ask the nearest competitive conditions you encounter, and how did you solve. Some time they will write simple code, and then let you detect race conditions code. Java can refer to the article on the conditions of competition before I posted. In my opinion this is one of the best java interview questions thread, it can detect the exact candidate to address the conditions of competition experience, or writing code which is free of data race or any other race condition. The best book in this regard is "Concurrency practices in Java".


10. How will you use the thread dump? How would you analyze Thread dump?

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


It will perform the run () method 11. Why do we call the start () method, why can not we directly call the run () method?

This is another very classic multithreaded java interview questions. This is what I have just started writing multithreaded programs confusing time. Now the problem is usually in a telephone interview or a middle-class Java in the first round of interviews asked. The answer to this question should be like this, when you call the start () method you will create a new thread, and execute code in the run () method inside. But if you just call the run () method, it does not create a new thread does not perform the calling thread code. "The difference between start and run approach" is written before I read this article for more information.


How do you wake up in 12.Java a blocked thread?

This is a problem on the thread and blocking tricky, it has many solutions. If you encounter a thread blocking IO, and I do not think there is a way to abort the thread. If the thread is blocked because the call to wait (), sleep (), or join () method caused, you can interrupt threads, and to wake it up by throwing InterruptedException. I wrote before "How to deal with blocking methods in java" There are a lot of information about the processing thread blocked.


13. What is the difference in Java CycliBarriar and CountdownLatch?

This thread problem is mainly used to detect whether you are familiar with the JDK5 and contracting. These two differences are CyclicBarrier reusable obstacle has been passed, but CountdownLatch can not be reused.


14. What is immutable, it has to write concurrent applications going to help?

Another multi-threaded classic interview question, not directly related with the thread, but indirectly help a lot. This java interview questions can become very tricky, if he asked you to write an immutable object, or ask you why String is immutable.


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

Multithreading and concurrent programs often have Memory-interface, race conditions, deadlocks, live locks and hunger encounter. The problem is not enough, if you get it wrong, it will be difficult to find and debug. This is the most based on interviews, rather than Java threading issues based on the actual application.


Finally,
we welcome the exchange, like the point of a praise yo remember the article, thanks for the support!


Guess you like

Origin blog.51cto.com/14442094/2432439