Home >Java >JavaInterview questions >Collection of classic Java interview questions (5)
1. What is thread synchronization and asynchronousness?
(More interview question recommendations: java interview questions)
Synchronization (synchronous) is coordinated pace, running in a predetermined order.
Asynchronous is the opposite of synchronization, it does not block and runs at the same time.
2. What is thread unsafe? How to solve? (Key points)
What is thread safety?
If there are multiple threads running at the same time in the process where your code is located, these threads may run this code at the same time. If the results of each run are the same as those of single-threaded runs, and the values of other variables are the same as expected, it is thread-safe.
In other words: The interface provided by a class or program is an atomic operation for threads or switching between multiple threads will not cause ambiguity in the execution results of the interface, which means that we do not need to Consider synchronization issues.
Thread safety issues are caused by global variables and static variables.
If there are only read operations and no write operations on global variables and static variables in each thread, generally speaking, this global variable is thread-safe; if multiple threads perform write operations at the same time, generally Thread synchronization needs to be considered, otherwise thread safety may be affected.
(Related tutorial recommendations: java introductory program)
Why is ArrayList thread unsafe? Why use it if it's not safe? How to solve thread unsafety?
In an ArrayList, when adding an element, it may be completed in two steps:
1. Store the element at the location of Items[Size];
2. Increase the value of Size.
In the case of single-threaded operation, if Size = 0, after adding an element, the element will be at position 0, and Size=1; and if it is multi-threaded, for example, there are two threads, Thread A first stores the element at position 0.
But at this time, the CPU schedules thread A to pause, and thread B gets a chance to run. Thread B also adds elements to this ArrayList, because Size is still equal to 0 at this time (note that we assume that adding an element requires two steps, and thread A only completed step 1), so thread B also adds elements to Stored at location 0. Then both thread A and thread B continue to run, both increasing the value of Size.
Okay, now let's take a look at the situation of ArrayList. There is actually only one element, stored at position 0, but Size is equal to 2. This is "thread unsafe".
How to solve?
Lock (object lock, lock code block), spin CAS method (optimistic locking), use the thread-safe data class provided by java
3. How to create a thread ? How many methods are there?
1. Inherit the Thread class
2. Implement the Runnable interface
3. Implement the Callable interface
4. Use Runnalbe Is the interface good? Or is it better to inherit the Thread class?
It is better to implement the Runnable interface.
1. Because implementing the Runnable interface can avoid the limitations of Java single inheritance.
When a class inherits Thread, it cannot inherit other classes. And when a class implements Runnable, it can also inherit from other classes.
2. More in line with object-oriented design
The run() method is used to encapsulate the code to be run by the thread. Then the object to which the run() method belongs is the thread task object. Subclass objects of the Thread class are both thread objects and thread task objects. The coupling is very strong.
With the Runnable interface, thread tasks and threads can be decoupled, improving the scalability of the program.
(Recommended related video tutorials: java video tutorial)
5. What is the difference between sleep() and wait()?
sleep is a method of the thread class (Thread), which causes this thread to suspend execution for a specified time and give execution opportunities to other threads, but the monitoring status is still maintained and will automatically resume after the time is up. Calling sleep does not release the object lock.
wait is a method of the Object class. Calling the wait method on this object causes this thread to give up the object lock and enter the waiting lock pool waiting for this object. Only after the notify method (or notifyAll) is issued for this object, this thread can Enter the object lock pool and prepare to obtain the object lock and enter the running state.
If the variable is declared volatile, it will be consistent with the main memory every time it is accessed; if the variable is accessed in a synchronized method or synchronized block, when the lock is obtained at the entrance of the method or block and the method or block The variables are synchronized when the lock is released on exit.
The above is the detailed content of Collection of classic Java interview questions (5). For more information, please follow other related articles on the PHP Chinese website!