Multithreading issues in Java Programmer interviews

Source: Internet
Author: User

Many of the core Java-based questions come from multithreading (multi-threading) and the collection Framework (collections framework), and the mastery of practical experience is necessary when it comes to understanding core threading concepts. This article collects some typical Java threading issues that are often asked by senior engineers.

0. What is multi-threaded synchronization in Java?

Under multi-threaded routines, 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 can lead to incorrect results for the program.

1, explain several ways to realize multithreading?

A Java thread can implement the Runnable interface or inherit the thread class to implement, and when you intend to multiply the inheritance, the first choice is to implement runnable.

2. What is the difference between Thread.Start () and Thread.run ()?

The Thread.Start () method (native) initiates the thread so that it enters a ready state, and the runtime executes the run () method when the CPU allocates time for the thread.

3, why do I need the run () and start () method, we can only use the run () method to complete the task?

We need run () &start () These two methods are because the JVM creates a separate thread that differs from the normal method's invocation, so this work is done by the thread's Start method, and start is implemented by the local method and needs to be called in the display. Another benefit of using these two methods is that any one object can be run as a thread, as long as the Runnable interface is implemented, which avoids the problem of multiple inheritance of Java resulting from inheriting the thread class.

4, what is the Threadlocal class, how to use it?

Threadlocal is a thread-level local variable, not a "local thread." Threadlocal provides a separate copy of the variable for each thread that uses the variable, and each thread modifies the copy without affecting the copy of the other thread object (the translator's note).

Here are the key points for thread local variables (ThreadLocal variables):

A thread local variable (ThreadLocal variables) conveniently provides a separate variable for each thread.

Threadlocal instances are typically present in a class as static private static fields that are used to associate a thread.

When multiple threads access the threadlocal instance, each thread maintains a separate copy of the variable provided by the threadlocal.

Common use can be seen in the DAO pattern, when the DAO class is a singleton class, the database link (connection) is maintained independently by each thread and does not affect each other. (Thread-based singleton)

5, when to throw invalidmonitorstateexception exception, why?

When you call any of the methods in Wait ()/notify ()/notifyall (), if the current thread does not get a lock on the object, it throws a Illegalmonitorstateexception exception ( This means that the program still tries to call Wait ()/notify ()/notifyall () when there is no synchronization block or synchronization method for the object. Because the exception is a subclass of Runtimeexcpetion, the exception does not have to be captured (although you can capture as long as you want). As runtimeexception, such exceptions will not be in wait (), notify (), Notifyall () The method signature mentioned.

6. What is the difference between Sleep (), suspend (), and wait ()?

Thread.Sleep () causes the current thread to be in a non-running (not Runnable) state at the specified time. The thread has been holding the object's monitor. For example, a thread is currently in a synchronous block or synchronous method, and other threads cannot enter the block or method. If another thread calls the interrupt () method, it wakes up the "sleeping" thread.

Note: Sleep () is a static method. This means that only the current thread is valid, and a common mistake is to call T.sleep (), where T is a different thread than the current thread. Even if T.sleep () is executed, the current thread goes to sleep, not the t thread. T.suspend () is an obsolete method that uses suspend () to cause a thread to go into a stagnant state, which will hold the object's monitor all the time, and suspend () can cause a deadlock problem.

Object.wait () makes the current thread out of a "non-operational" state, unlike sleep () where wait is the method of object instead of thread. When calling Object.wait (), the thread first acquires an object lock on the object, the current thread must remain in sync on the lock object, add the current thread to the wait queue, and then another thread can synchronize the same object lock to invoke Object.notify (), which will wake up the threads in the original wait. Then release the lock. Basically wait ()/notify () is similar to sleep ()/interrupt (), except that the former needs to acquire an object lock.

7. What happens when I use synchronization on a static method?

When a static method is synchronized, the class object is obtained, so when a thread enters a synchronous static method, the thread monitor acquires the object lock of the class itself, and other threads cannot enter any static synchronization methods of the class. It is not like an instance method, because multiple threads can access different instance synchronization instance methods at the same time.

8. When a synchronous method has been executed, can the thread invoke 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 have any checks for the unsynchronized method, and the lock object is only checked in the synchronous method or in the synchronous code block. If a method is not declared to be synchronous, even if you are using shared data Java will still be called, but do not check whether it is safe, so in this case special care. Whether a method is declared as synchronous depends on critical section access (critial section Access), and it is not necessary to declare the method to be synchronous if it does not access critical sections (shared resources or data structures).

9. Can two threads invoke two different synchronous instance methods on an object?

No, because an object has synchronized an instance method, the thread acquires an object lock on the object. Therefore, the other synchronization methods can be performed only after the method has been executed to release the object lock.

10. What is a deadlock

A deadlock is when two or more threads are blocked indefinitely, and the threads wait for the required resources between them. This can happen when two threads try to acquire locks for other resources, and each thread is trapped in an infinite wait for other resource locks to be released, unless a user process is terminated. As far as JAVAAPI is concerned, thread deadlock can occur in a situation.

When two threads call one another thread.join ()

When two threads use nested synchronization blocks, one thread consumes the required lock on another thread, and it is possible to deadlock while waiting for each other to become blocked.

11. What is a thread starved and what is a live lock?

Thread starvation and live lock while not wanting to be a deadlock-like common problem, for concurrent programming designers it is like an encounter.

Non-blocking threads make resources available when all threads are blocked, or because the required resources are invalid and cannot be processed. Javaapi midline Cheng locks can occur in the following situations:

When all threads execute object.wait (0) in the program, the wait method with a parameter of 0. The program will have a live lock until the thread calls Object.notify () or Object.notifyall () on the corresponding object.

When all lines are measuring modules in an infinite loop.

The question here is not exhaustive, and I believe there are many important questions that are not mentioned, and what questions do you think should be included? You are welcome to share any form of questions and suggestions in the comments.

I'm the dividing line of the king of the Land Tiger.

Reference: http://blog.jobbole.com/18571/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.