Interview Questions _1_to_16_ multi-threading, concurrency, and threading Fundamentals

Source: Internet
Author: User
Tags volatile

The basic problems of multithreading, concurrency, and threading

1) Can I create a volatile array in Java?
Yes, you can create an array of volatile types in Java, but only a reference to an array, not an entire array. I mean, if you change the array that the reference points to, it will be protected by volatile, but if multiple threads change the elements of the array at the same time, the volatile identifier will not be able to act as a protection.

2) Does volatile make a non-atomic operation into an atomic operation?
A typical example is a long type member variable in a class. If you know that the member variable will be accessed by multiple threads, such as counters, prices, etc., you'd better set it to volatile. Why? Because reading a long type variable in Java is not atomic, it needs to be divided into two steps, and if one thread is modifying the value of the long variable, the other thread may see only half of that value (the first 32 bits). However, reading and writing to a volatile long or double variable is atomic.

3) What is the practice of volatile modifiers?
One practice is to use volatile to modify long and double variables so that they can be read and written by atomic type. Double and long are 64 bits wide, so for these two types of reading is divided into two parts, the first read the first 32 bits, and then read the remaining 32 bits, the process is not atomic, but the Java volatile type long or double variable read and write is atomic. Another effect of the volatile modifier is to provide memory barriers (barrier), such as applications in distributed frameworks. Simply put, it is when you write a volatile variable that the Java memory model inserts a write barrier (write barrier), and before reading a volatile variable, it inserts a reading barrier (read barrier). This means that when you write a volatile domain, you can ensure that any thread can see the value you write, and that the update of any value is visible to all threads before writing, because the memory barrier updates all other write values to the cache.

4) What are the guarantees for volatile type variables? Answer
Volatile variables provide order and visibility guarantees, for example, the JVM or JIT reordering statements for better performance, but the volatile type variable is not reordered with other statements even if there are no synchronized blocks. Volatile provides happens-before guarantees that a thread's modifications can be visible to other threads. In some cases, volatile can also provide atomicity, such as reading 64-bit data types, such as long and double are not atomic, but the volatile type of double and long is atomic.

5) 10 Threads and 2 threads of synchronization code, which is easier to write?
From the point of writing code, the complexity of the two is the same, because the synchronization code and the number of threads are independent of each other. But the choice of synchronization policy depends on the number of threads, because the more threads mean greater competition, the more complex code and expertise you need to leverage synchronization techniques such as lock separation.

6) How do you call the Wait () method? Do you use an if block or a loop? Why? Answer
The wait () method should be called in a loop, because when the thread gets to the CPU to start executing, other conditions may not be met, so it is better to have the loop detection condition satisfied before processing. The following is a standard code for using the wait and notify methods:

123456 // The standard idiom for using the wait methodsynchronized(obj) {while(condition does not hold)obj.wait(); // (Releases lock, and reacquires on wakeup)... // Perform action appropriate to condition}

See effective Java 69th For more information on why the wait method should be called in a loop.

7) What is pseudo-sharing in a multithreaded environment (false sharing)?
Pseudo-sharing is a well-known performance issue in multithreaded systems where each processor has its own local cache. Pseudo-sharing threads that occur on different processors are dependent on the same cache line for the modification of the variable, as shown in:

Java face test for experienced programmers

Pseudo-sharing problems are difficult to find because threads can access completely different global variables, but in memory they happen to be in very similar positions. As with many other concurrency problems, the most basic way to avoid pseudo-sharing is to carefully review the code and adjust your data structure according to the cache rows.

8) What is Busy spin? Why should we use it?
BUSY Spin is a technology that waits for events on the basis of not releasing the CPU. It is often used to avoid losing data in the CPU cache (if the thread pauses first, then runs on other CPUs are lost). So, if your job requires low latency, and your threads are not currently in any order, you can instead call the sleep () or wait () method by looping through new messages in the queue. The only benefit is that you just have to wait a short time, such as a few microseconds or a few nanoseconds. The LMAX distributed framework is a library of high-performance inter-thread communication that has a busyspinwaitstrategy class that is based on this concept and uses the busy spin loop eventprocessors to wait for the barrier.

9) How do I get a thread dump file in Java?
Under Linux, you can get the dump file for your Java application by ordering the kill-3 PID (the process ID of the Java process). Under Windows, you can press Ctrl + BREAK to get. This allows the JVM to print the thread's dump file to a standard output or error file, which may be printed in the console or log file, depending on the configuration of the application. If you use Tomcat.

Swing is thread-safe? Answer
No, Swing is not thread-safe. You cannot update Swing components, such as JTable, JList, or JPanel, through any thread, in fact, they can only be updated by GUI or AWT threads. That's why Swing provides the invokeandwait () and Invokelater () methods to get GUI update requests from other threads. These methods put the update request into the AWT thread queue, can wait all the time, or return the results directly through an asynchronous update. You can also view and learn more in the reference answer.

11) What are thread local variables? Answer
A thread local variable is a variable that is confined to the thread itself and is owned by the thread itself and is not shared among multiple threads. Java provides the ThreadLocal class to support thread-local variables, which is a way to implement thread safety. However, when using thread-local variables in a managed environment (such as a Web server), you should be particularly careful, in which case the worker thread has a longer life cycle than any application variable. Once any thread local variable is not released after the work is completed, there is a risk of a memory leak in the Java application.

12) write a piece of code with wait-notify to solve the producer-consumer problem? Answer
Please refer to the sample code in the answer. Just remember to call the Wait () and notify () methods in the synchronization block, and if blocked, test the wait condition by looping.

13) Write a thread-safe singleton mode (Singleton) in Java? Answer
Please refer to the sample code in the answer, which is a step-by-step guide to creating a thread-safe Java Singleton class. When we say thread safety, it means that even if initialization is in a multithreaded environment, a single instance can still be guaranteed. In Java, using enumerations as singleton classes is the simplest way to create a thread-safe singleton pattern.

What is the difference between the sleep method and the Wait method in Java? Answer
While both are used to pause the currently running thread, sleep () is actually just a short pause because it does not release the lock, and wait () means that the condition waits, which is why the method is releasing the lock because the other waiting thread can get to the lock when the condition is met.

15) What are immutable objects (immutable object)? How do I create an immutable object in Java? Answer
Immutable objects means that once an object is created, the state can no longer be changed. Any modifications will create a new object, such as String, Integer, and other wrapper classes. For details, see the answer and step-by-step instructions to create an immutable class in Java.

16) Can we create an immutable object that contains Mutable objects?
Yes, we can create an immutable object that contains Mutable objects, you just have to be careful not to share references to mutable objects, and if you need to change, return a copy of the original object. The most common example is a reference to an object that contains a Date object.

Face question _1_to_16_ multi-threading, concurrency, and threading Fundamentals

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.