The most difficult Java interview question in history

The topics are as follows:

The output of the program?

Program output results

or

Investigate knowledge points

In Java, multithreaded programs are the most difficult to understand and debug. Many times, the execution results are not executed as we think. Therefore, it is particularly difficult to Multithread in Java. I vaguely remember that when I took the C language level 2 test in college, what was the topic + + and many other priorities, and asked the final output results. This kind of topic wanted to test some operator priority and combination problems. That's OK, but Java multithreading still needs to be well understood. The backrest doesn't work.

Let's start with a simple analysis:

The topic involves two threads (main thread and sub thread), and the keywords involve synchronized and thread.sleep. The synchronized keyword is still complex (it may not be understood in place sometimes, so the above topic will be misunderstood). Its role is to realize line synchronization (there are many ways to implement thread synchronization, which will be mentioned in subsequent articles. Others need to study some implementations of the great God 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 (actually a pessimistic strategy), so as to ensure that threads only remember security.

Usage of general keyword synchronized

In the above code, the synchronized usage actually belongs to the second case. Direct action on instance method: it is equivalent to locking the current instance. Before entering the synchronization code, you need to obtain the lock of the current instance.

Possible misunderstandings

1. Because we don't understand synchronized, many times, our multithreads operate a synchronized method. When two threads call two different synchronized methods, we think it doesn't matter. This idea is misunderstood. Direct action on instance method: it is equivalent to locking the current instance. Before entering the synchronization code, you need to obtain the lock of the current instance.

2. If one calls the synchronized method. There is no relationship between the other method and the ordinary method, and there is no waiting relationship between the two methods. These are very useful for the later analysis.

Thread. sleep

Pause the execution of the current thread (that is, the thread calling the method) for a period of time, so that other threads have a chance to continue execution, but it does not release the object lock. That is, if synchronized synchronization is fast, other threads still cannot access the shared data. Note that this method should catch exceptions, which is very useful for subsequent analysis.

Analysis process

Java is executed from the main method. It is said that there are two threads, but it is useless to modify the thread priority here. The priority is only when both programs have not been executed. Now, as soon as the code is executed, the main thread has been executed. For the attribute variable int b = 100, there will be no visibility problem due to the use of synchronized (there is no need to use volatile declaration). When step 1 is executed (thread t = new thread (TT)// 1) The thread is in the new state and has not started working. When step 2 is executed (t.start()// 2) When the start method is called, the thread is really started and enters the runnable state. The runnable state indicates that it 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 performing step 3, you must obtain the lock first (since start needs to call the native method and everything is ready after use, 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, and then it will call the run method and execute the M1 method). In fact, it doesn't matter whether the thread.sheet in the two synchronized methods doesn't matter. It is estimated that it is to increase confusion Add difficulty. In fact, the sub thread is ready soon when step 3 is executed. However, due to the existence of synchronized and the same object, the sub thread has to wait. Since the execution sequence in the main method is sequential, it must be step 3 before it can go to step 4. Since step 3 is completed, the sub thread can execute M1. Here, there is a problem of who gets the multi thread first. If step 4 gets it first, then main thread B = 2000. If sub thread M1 gets it, then B may have been assigned to 1000 or it may have been output before step 4. The possible result is main thread B = 1000 or main thread B = 2000, Here, if the 6 steps are removed, then B = execution first and main thread B = first are uncertain. However, due to the existence of 6 steps, the main thread B = is in the front anyway, so it is equal to 1000 or 2000, depending on the situation, and then B = 1000 must be fixed.

Some suggestions for multithreading

There are also some skills to share in the follow-up articles. Multithreading is particularly important and difficult. I hope you will pay more attention to it.

Some debugging skills of multithreading

Because of the breakpoint, all threads need to stop when they pass through the breakpoint, which leads to the continuous interruption of this point. It's very uncomfortable. There are conditional breakpoints in Eclispe. You can stop when the conditions are met, so it's convenient.

summary

The above is the most difficult Java interview question in the history introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>