Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common multithreaded interview questions in JAVA?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this article, the editor introduces in detail "what are the common multithreaded interview questions in JAVA", with detailed content, clear steps and proper handling of details. I hope that this article "what common multithreaded interview questions in JAVA" can help you solve your doubts.

1. What is a thread?

Thread is the smallest unit that the operating system can schedule. It is included in the process and is the actual operating unit in the process. Programmers can use it for multiprocessor programming, and you can use multithreading to speed up compute-intensive tasks. For example, if it takes 100 milliseconds for a thread to complete a task, it only takes 10 milliseconds for ten threads to complete the changed task.

2. what's the difference between a thread and a process?

Threads are a subset of processes, and a process can have many threads, each of which performs different tasks in parallel. Different processes use different memory space, while all threads share the same memory space. Each thread has a separate stack memory for storing local data.

3. How to implement threads in Java?

There are two ways: the instance of the java.lang.Thread class is a thread but it needs to call the java.lang.Runnable interface to execute, because the thread class itself is the calling Runnable interface, so you can inherit the java.lang.Thread class or directly call the Runnable interface to override the run () method to implement the thread.

4. The function and difference between Java keyword volatile and synchronized?

Volatile:

The variables it modifies do not retain copies and directly access the ones in the main memory.

In the Java memory model, there is main memory, and each thread has its own memory (such as registers). For performance, a thread maintains a copy of the variable to be accessed in its own memory. At some point in the same variable, the value in the memory of one thread may be inconsistent with the value in the memory of another thread, or the value in main memory. A variable declared as volatile means that the variable can be modified by other threads at any time, so it cannot be cache in the thread memory.

Synchronized:

When it is used to modify a method or a block of code, it ensures that at most one thread executes the code at a time.

First, when two concurrent threads access the synchronized (this) synchronous code block in the same object object, only one thread can be executed at a time. Another thread must wait for the current thread to finish executing the code block before it can be executed.

However, when one thread accesses a synchronized (this) synchronization block of object, another thread can still access a non-synchronized (this) synchronization block in that object.

Third, it is especially critical that when one thread accesses a synchronized (this) synchronous code block of object, the access of other threads to all other synchronized (this) synchronous code blocks in object will be blocked.

4. When a thread accesses a synchronized (this) synchronization block of object, it acquires the object lock of the object. As a result, access by other threads to all parts of the synchronous code of the object object is temporarily blocked.

5. The above rules are also applicable to other object locks.

5. What are the different thread life cycles?

When we create a new thread in the Java program, its state is New. When we call the thread's start () method, the state is changed to Runnable. The thread scheduler allocates CPU time to threads in the Runnable thread pool and changes their state to Running. Other thread states are Waiting,Blocked and Dead.

6. What is your understanding of thread priority?

Every thread has a priority. Generally speaking, high-priority threads have priority at run time, but this depends on the implementation of thread scheduling, which is OS dependent related to the operating system.

We can define the priority of the thread, but this does not guarantee that the high-priority thread will execute in front of the lower-priority thread. The thread priority is an int variable (from 1 to 10), with 1 for the lowest priority and 10 for the highest priority.

7. What is a Deadlock? How to analyze and avoid deadlocks?

A deadlock is a situation in which more than two threads are permanently blocked, which requires at least two or more threads and more than two resources.

To analyze the deadlock, we need to look at the thread dump of the Java application. We need to find out which threads are in the state of BLOCKED and the resources they are waiting for. Each resource has a unique id, and with this id we can find out which threads already have its object lock.

Avoiding nested locks, using locks only where they are needed, and avoiding waiting indefinitely are common ways to avoid deadlocks.

8. What is thread safety? is Vector a thread safety class?

If your code is in a process where multiple threads are running at the same time, those threads may run the code at the same time. If the result of each run is the same as that of a single thread, and the values of other variables are the same as expected, it is thread-safe.

The same instance object of a thread-safe counter class does not make a calculation error when it is used by multiple threads. Obviously you can divide the collection classes into two groups, thread-safe and non-thread-safe. Vector uses synchronous methods to achieve thread safety, while similar ArrayList is not thread safe.

9. How to stop a thread in Java?

Java provides rich API but does not provide API for stopping threads. JDK 1.0 originally had some control methods like stop (), suspend () and resume (), but they were deprecated in subsequent versions of JDK due to potential deadlock threats, and then Java API designers did not provide a compatible and thread-safe way to stop a thread.

The thread ends automatically when the run () or call () method finishes. If you want to end a thread manually, you can use the volatile Boolean variable to exit the loop of the run () method or cancel the task to interrupt the thread.

10. What is ThreadLocal?

ThreadLocal is used to create thread local variables, and we know that all threads of an object will share its global variables, so these variables are not thread safe, and we can use synchronization techniques. But when we don't want to use synchronization, we can choose the ThreadLocal variable.

Each thread has its own Thread variable, and they can use the get () set () method to get their default values or change their values within the thread. ThreadLocal instances typically want them to be associated with thread state as private static attributes.

After reading this, the article "what are the common multithreaded interview questions in JAVA" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report