In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the common multithreaded interview questions in JAVA". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the common multithreaded interview questions in JAVA?"
What are thread local variables?
Thread local variables are variables that are limited within the thread, belong to the thread itself, and are not shared among multiple threads. Java provides the ThreadLocal class to support thread local variables, which is a way to achieve thread safety. However, special care should be taken when using thread local variables in a managed environment (such as a web server), in which case the lifetime of a worker thread is longer than that of any application variable. Once any thread local variable is not released after the work is completed, the Java application will have the risk of memory leakage.
Runnable or Thread?
Everyone knows that we can implement threads by inheriting the Thread class or calling the Runnable interface. The question is, which method is better? Under what circumstances do you use it? This question is easy to answer if you know that Java does not support multiple inheritance of classes, but allows you to call multiple interfaces. So if you want to inherit other classes, call the Runnable interface, of course.
What is the difference between the start () and run () methods in the Thread class?
The start () method is used to start the newly created thread, and the run () method is called internally by start (), which is different from calling the run () method directly. When you call the run () method, it will only be called in the original thread, and no new thread will start the start () method will start the new thread.
What is the difference between Runnable and Callable in Java?
Both Runnable and Callable represent tasks that are executed in different threads. Runnable has been around since JDK1.0, and Callable has been added in JDK1.5. The main difference is that the call () method of Callable can return a value and throw an exception, while the run () method of Runnable does not. Callable can return a Future object loaded with the results of the calculation.
What's the difference between CyclicBarrier and CountDownLatch?
Both CyclicBarrier and CountDownLatch can be used to make one set of threads wait for other threads. Unlike CyclicBarrier, CountdownLatch cannot be reused.
What is the Java memory model?
The Java memory model defines and guides the deterministic behavior of Java programs among different memory architectures, CPU, and operating systems. It is especially important in the case of multithreading. The changes made by the Java memory model to one thread are guaranteed to be visible to other threads, and there is a prior relationship between them. This relationship defines some rules that make programmers think more clearly when programming concurrently. For example, having sex in advance ensures that:
The code within a thread can be executed sequentially, which is called a program order rule.
For the same lock, an unlock operation must occur before another lock operation that occurs after the time, also known as the pipe locking rule.
The previous write operation to volatile precedes the read operation of the latter volatile, which is also called the volatile variable rule.
Any operation within a thread must be called a thread startup rule after the thread's start () call.
All operations of a thread terminate the rule before the thread terminates.
The finalization of an object must be called an object termination rule after the construction of the object is completed. Transitivity
What is the volatile variable in Java?
Volatile is a special modifier that can only be used by member variables. In the absence of synchronization classes in Java concurrent programs, multithreading operations on member variables are transparent to other threads. The volatile variable guarantees that the next read operation will occur after the previous write operation, which is the volatile variable rule of the previous question.
What is thread safety? Is Vector a thread-safe class?
* * if your code is in a process where multiple threads are running at the same time, these 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 will 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.
What is the race condition in Java? Give me an example.
Race conditions can cause programs to have some bugs in concurrency. When multithreading competes for some resources, there will be race conditions. If the program to be executed first fails, then there will be some uncertain bugs in the whole program. This bugs is hard to find and repeats because of random competition between threads.
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.
What happens when an exception occurs while a thread is running?
Simply put, if the exception is not caught, the thread will stop execution. Thread.UncaughtExceptionHandler is an embedded interface for handling situations where uncaught exceptions cause a sudden interruption of a thread. When an uncaught exception will cause a thread to be interrupted, JVM uses Thread.getUncaughtExceptionHandler () to query the thread's UncaughtExceptionHandler and pass the thread and exception as parameters to the handler's uncaughtException () method for processing.
What is the difference between notify and notifyAll in Java?
Because multithreading can wait for a single monitor lock, Java API designers provide methods to notify them when waiting conditions change, but these methods are not fully implemented. The notify () method cannot wake up a specific thread, so only a thread has a chance to exercise its talents while it is waiting. NotifyAll () wakes up all threads and allows them to compete for locks to ensure that at least one thread continues to run.
Thank you for your reading, the above is the content of "what are the common multithreaded interview questions in JAVA". After the study of this article, I believe you have a deeper understanding of the common multithreaded interview questions in JAVA, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.