The most difficult Java interview questions in history
The questions are as follows:
public class TestSync2 implements Runnable { int b = 100; synchronized void m1() throws InterruptedException { b = 1000; Thread.sleep(500); //6 System.out.println("b=" + b); } synchronized void m2() throws InterruptedException { Thread.sleep(250); //5 b = 2000; } public static void main(String[] args) throws InterruptedException { TestSync2 tt = new TestSync2(); Thread t = new Thread(tt); //1 t.start(); //2 tt.m2(); //3 System.out.println("main thread b=" + tt.b); //4 } @Override public void run() { try { m1(); } catch (InterruptedException e) { e.printStackTrace(); } }}
Output result of the program?
Program output result
main thread b=2000b=1000
Or
main thread b=1000b=1000
Knowledge points
- Synchronize instance lock.
- Memory visibility under concurrency.
In java, multi-threaded programs are the most difficult to understand and Debug. In many cases, the execution results are not as expected. Therefore, java multithreading is particularly difficult. I vaguely remember when I took the C language level 2 test in college, what is the question in the question ++ and many other priorities, and I asked the final output result together, this type of question is intended to test the priority and integration of operators. That back-to-back is enough, but java multithreading still needs to be well understood. Back-to-back is not enough.
The following is a simple analysis:
This topic involves two threads (main and sub-threads) and keywords synchronized and Thread. sleep.
The synchronized keyword is still complicated (sometimes it is not understood in place, so the above question may be a bit wrong). Its role is to implement thread synchronization (there are many methods to implement thread synchronization, it is just a later article that will talk about other things. You need to study some implementations of Doug Lea.) its job is to lock the code to be synchronized, so that only one thread can enter the synchronization block at a time (in fact, it is a pessimistic Policy) to ensure that the thread only remembers the security.
General keywords synchronized usage
- Specify the Lock Object: Lock the given object to enter the lock of the given object that needs to be active before the synchronization code.
- Directly acting on the instance method: It is equivalent to locking the current instance and obtaining the lock of the current instance before entering the synchronization code.
- Directly acting on static methods: It is equivalent to locking the current class and obtaining the lock of the current class before entering the synchronization code.
The above code, synchronized usage is actually the second case. Directly acting on the instance method: It is equivalent to locking the current instance and obtaining the lock of the current instance before entering the synchronization code.
Possible misunderstandings
1. because synchronized is not understood, many times, we use multiple threads to operate a synchronized method. When two threads call two different synchronized methods, there is no such idea. Directly acting on the instance method: It is equivalent to locking the current instance and obtaining the lock of the current instance before entering the synchronization code.
2. If one calls the synchronized method. There is no relation between calling common methods, and there is no waiting relationship between the two.
These are very useful for subsequent analysis.
Thread.sleep
Pause the execution of the current thread (that is, the thread that calls this method) for a period of time, so that other threads can continue to execute, but it does not release the object lock. That is to say, if synchronized is fast, other threads still cannot access Shared data. Note that this method is useful for subsequent analysis to capture exceptions.
Analysis Process
Java is executed from the main method. As mentioned above, there are two threads, but it is useless to modify the thread priority here, the priority is obtained only when two programs are not executed. Now the main thread has been executed as soon as the code is executed. The attribute variable int B = 100 does not have visibility issues because synchronized is used (and it is not necessary to declare volatile ), when Step 1 is executed (Thread t = new Thread (tt); // 1) the Thread is in the new State and has not started working yet. When performing Step 2 (t. start (); // 2) when the start method is called, the thread is actually started and enters the runnable state. The runnable State indicates that the thread can be executed and everything is ready, but it does not mean that it must be executed on the cpu. Whether it is actually executed depends on the scheduling of the Service cpu. Here, when step 3 is executed, the lock must be obtained first (because start needs to call the native method, and after the use is complete, everything is ready, but it does not necessarily mean that it must be executed on the cpu, whether or not the run method is actually executed depends on the scheduling of the Service cpu, and then the run method is called to execute the m1 method ). Here, the Thread. sheep in the two synchronized methods does not matter. It is estimated that it will increase the difficulty of obfuscation. During step 3 execution, the sub-thread is ready soon. However, because synchronized exists and acts on the same object, the sub-thread has to wait. Because the execution sequence in the main method is sequential, steps 4 can only be performed after Step 3 is completed. Because step 3 is completed, the Child thread can execute m1. Here there is a multi-thread problem who gets it first. If we get it in Step 4, main thread B = 2000, if the m1 of the subthread is obtained, it may be that B has been assigned a value of 1000 or has not been assigned a value of 4. The possible result is that main thread B = 1000 or main thread B = 2000, if Step 6 is removed here, B = is not sure before and before the execution of main thread B =. However, because step 6 exists, no matter how the main thread B = is in the front, it is equal to 1000 or 2000. Then, B = 1000 is fixed.
Multithreading suggestions
- The thread is also very precious, so it is recommended to use the thread pool, the thread pool is used a lot, it is particularly important to prepare for sharing in the future, you need to be aware of it.
- Give the thread a name. When the online cpu usage is high, advanced jstack is required. It is much more convenient if there is a name.
- Special attention should be paid to thread security issues for multithreading. We also need to know that jdk is thread security insecure, so there will be no inexplicable problems during use.
There are also some tips to share later articles. multithreading is particularly important and difficult. I hope you will spend more time on it.
Multi-thread debugging skills
Because of the breakpoint, all threads need to be stopped when they pass through the breakpoint. As a result, this point is broken continuously and it is very uncomfortable. The conditional breakpoint exists in eclispe and can be stopped when the conditions are met, this is convenient.
Summary
The above is the most difficult Java interview question in history introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!