Multi-thread problem during Java programmer interview, java multi-thread
Many core Java interview questions come from Multi-Threading and collection Framework. skillful practical experience is required to understand the concept of core threads. This article collects typical Java thread problems that are frequently asked by senior engineers.
0. What is multithreading synchronization in Java?
In a multi-threaded program, synchronization can control access to shared resources. If there is no synchronization, when a Java thread modifies a shared variable, another thread is using or updating the same variable, which may easily lead to program errors.
1. How can I implement multithreading?
A Java Thread can implement the Runnable interface or inherit the Thread class. When you plan to implement multiple inheritance, the Runnable is preferred.
2. What is the difference between Thread. start () and Thread. run?
The Thread. start () method (native) starts the Thread and enters the ready state. When the cpu allocates time for this Thread, the JVM schedules the run () method.
3. Why do we need the run () and start () Methods? Can we use the run () method to complete the task?
We need the run () & start () methods because the JVM creates a separate thread, which is different from the call of a common method. Therefore, this work is done by the start method of the thread, start is implemented by local methods and needs to be displayed and called. Another advantage of using these two methods is that any object can be used as a line operation, as long as the Runnable interface is implemented, this avoids the Java multi-Inheritance Problem Caused by inheriting the Thread class.
4. What is the ThreadLocal class and how to use it?
ThreadLocal is a local variable at the Thread level, not a "local thread ". ThreadLocal provides an independent copy of the variable for each thread that uses the variable. Modifying the copy of each thread does not affect the copies of other thread objects ).
The following are the key points of ThreadLocal variables:
ThreadLocal variables provides a separate variable for each thread.
ThreadLocal instances are usually used as static private static fields in a class, which is used to associate a thread.
When multiple threads access the ThreadLocal instance, each thread maintains an independent copy of the variable provided by ThreadLocal.
Common use can be seen in the DAO mode. When the DAO class is used as a singleton class, the connection is maintained independently by each thread, without affecting each other. (Thread-based Singleton)
5. When will an InvalidMonitorStateException be thrown? Why?
If the current thread does not obtain the Lock of this object when calling any method in wait ()/notify ()/notifyAll, then the IllegalMonitorStateException will be thrown (that is, when the program still tries to call wait ()/notify ()/notifyAll () When no synchronization block or synchronization method of the object is executed ). This exception is a subclass of RuntimeExcpetion, so it does not have to be captured (although you can capture it as long as you want ). as a RuntimeException, such exceptions will not be raised in the method signature of wait (), Y (), policyall.
6. What is the difference between Sleep (), suspend (), and wait?
Thread. sleep () puts the current Thread in the Not Runnable state at the specified time. The thread keeps holding the monitor of the object. For example, if a thread is currently in a synchronization block or method, other threads cannot enter the block or method. If another thread calls the interrupt () method, it will wake up the "Sleep" thread.
Note: sleep () is a static method. This means that it is only valid for the current thread. A common error is the call of t. sleep (). (here t is a thread different from the current thread ). Even if you execute t. sleep (), the current thread goes to sleep, not the t thread. T. suspend () is an out-of-date method. Using suspend () causes the thread to stop. This thread will always hold the monitor of the object, and suspend () is prone to deadlock.
Object. wait () causes the current thread to be out of the "not running" state. Unlike sleep (), wait is the object method rather than the thread. Call object. when wait () is used, the thread first needs to obtain the object lock of this object. The current thread must keep synchronizing the Lock Object and add the current thread to the waiting queue, then another thread can synchronize the same object lock to call the object. Y (), which will wake up the thread in the waiting state and then release the lock. Basically, wait ()/notify () is similar to sleep ()/interrupt (), except that the former needs to obtain the object lock.
7. What happens when I use synchronization in static methods?
When synchronizing static methods, the "Class" object of the Class is obtained. Therefore, when a thread enters the static method of synchronization, the thread monitor obtains the object lock of the Class, other threads cannot enter any static synchronization method of this class. Unlike the instance method, because multiple threads can simultaneously access different instance synchronization instance methods.
8. When a synchronization method has been executed, can the thread call the non-synchronous instance method on the object?
Yes. A non-synchronous method can always be called without any problems. In fact, Java does not perform any check for non-synchronous methods, and the lock object is only checked in the synchronous method or synchronous code block. If a method is not declared as synchronous, it will still be called even if you are using shared data Java, rather than checking whether it is secure, so be careful in this case. Whether a method is declared as synchronous depends on the critial section access. If the method does not access the critical section (shared resource or data structure), it is unnecessary to declare it as synchronous.
9. Can two threads on an object call two different synchronous instance methods?
No, because an object has synchronized the instance method, the thread obtains the object lock of the object. Therefore, other synchronization methods can be executed only after the method is executed to release the object lock.
10. What is a deadlock?
A deadlock means that two or more threads are infinitely blocked, and each thread waits for the required resources. This situation may occur when two threads attempt to obtain the Lock of other resources, and each thread falls into an infinite waiting for the release of the lock of other resources, unless a user process is terminated. For Java API, the thread deadlock may occur in the following situations.
● When two threads call Thread. join ()
● When two threads use nested synchronization blocks, one thread occupies the required locks of the other thread and is blocked when waiting for each other, a deadlock may occur.
11. What is a thread starved to death, and what is a live lock?
Although thread starvation and live locks do not want to be the same common problems as deadlocks, they are the same for concurrent programming designers.
When all threads are blocked, or the required resources are invalid and cannot be processed, there is no non-blocking thread to make the resources available. The thread lock in Java API may occur in the following situations:
● When all threads execute Object. wait (0) in the program, the parameter is 0 for the wait method. The program will send a live lock until there is a thread on the corresponding Object that calls Object. Every y () or Object. Every yall ().
● When all threads are stuck in an infinite loop.
The questions here are not detailed. I believe there are still many important questions that have not been mentioned. What other questions do you think should be included in this question? You are welcome to share any questions and suggestions in the comments.
I am the dividing line of tiantiao
Reference: http://blog.jobbole.com/18571/
What should I pay attention to when interviewing java programmers? How can I pass
Work experience is very important, followed by skills!
Because the interview will ask you what, you can't do your best!
When I was looking for a job, he asked me if I had any experience or did not do anything. I said no! Just graduated! I think I am too honest, but I also tell the truth! Later, when he spoke to me, He became impatient and asked nothing. He said that our company would hire experienced people to start work when they could come up!
I thought I was forced to slide my head because of repeated failures. They asked me if I had any experience and I said yes. Although I didn't, I certainly had the preparation, I have asked my predecessors what they have done in the company, and I have taken over all the links in charge of some projects, and he cannot check if I have worked there, of course, because there is no such thing, it is a bit of a guilty conscience. But I must have done it! You have no better experience than you said!
Too many situations have been encountered, and many questions will be asked during the interview. I have previously been asked what to do if the session value is too large to be stored and lost; there are several types of database deletion, what is the difference; java multithreading; how about the algorithm? Let's compile a bubble sort for me on the site. There are also some written tests, few written java, many of which are databases, JQ, JS, and so on. In short, these skills are required.
But really, experience is very important!
What should I pay attention to when interviewing java programmers? How can I pass
Work experience is very important, followed by skills!
Because the interview will ask you what, you can't do your best!
When I was looking for a job, he asked me if I had any experience or did not do anything. I said no! Just graduated! I think I am too honest, but I also tell the truth! Later, when he spoke to me, He became impatient and asked nothing. He said that our company would hire experienced people to start work when they could come up!
I thought I was forced to slide my head because of repeated failures. They asked me if I had any experience and I said yes. Although I didn't, I certainly had the preparation, I have asked my predecessors what they have done in the company, and I have taken over all the links in charge of some projects, and he cannot check if I have worked there, of course, because there is no such thing, it is a bit of a guilty conscience. But I must have done it! You have no better experience than you said!
Too many situations have been encountered, and many questions will be asked during the interview. I have previously been asked what to do if the session value is too large to be stored and lost; there are several types of database deletion, what is the difference; java multithreading; how about the algorithm? Let's compile a bubble sort for me on the site. There are also some written tests, few written java, many of which are databases, JQ, JS, and so on. In short, these skills are required.
But really, experience is very important!