Simple analysis of Java interview challenges __java

Source: Internet
Author: User
Tags instance method

let's start with a simple analysis


This topic involves 2 threads (main thread main, child thread), keyword involves synchronized, thread.sleep.
Synchronized keyword or more complex (may sometimes not understand in place so the above topic will be a bit mistaken), his role is to achieve thread synchronization (to achieve thread synchronization there are many ways, it is just a follow-up article will say other, need to study the great God Doug Lea some of the implementation), Its job is to lock the code that needs to be synchronized so that only one thread can enter the sync block (in fact, a pessimistic policy) each time to ensure that the thread remembers security only.


usage of general keyword synchronized


Specifies the lock object: Locks a given object and requires a live given object before entering the synchronization code.

Acts directly on an instance method: the equivalent of locking the current instance to get the current instance's lock before entering the synchronization code.

Works directly on static methods: the equivalent of locking the current class and getting the current class lock before entering the synchronization code.


The code above, synchronized usage is actually the second case. Acts directly on an instance method: the equivalent of locking the current instance to get the current instance's lock before entering the synchronization code.


a possible misunderstanding


1. Because of the synchronized understanding, because many times, we are multithreading is to operate a synchronized method, when 2 threads to call 2 different synchronized methods, it is not related to the idea that there is a misunderstanding. Acts directly on an instance method: the equivalent of locking the current instance to get the current instance's lock before entering the synchronization code.


2. If one calls the Synchronized method. Another call to the normal method is not a relationship, 2 is not a waiting relationship.


These are useful for the subsequent analysis.


Thread.Sleep


Causes the current thread (that is, the thread that calls the method) to suspend execution for a period of time so that other threads have the opportunity to continue executing, but it does not release object locks. That is, if a synchronized synchronization is fast, other threads still cannot access the shared data. Note that this method captures the exception and is useful for subsequent analysis.


Analysis Process


Java is done from the main method, which says there are 2 threads, but even if you modify the thread priority is useless, the priority is not in the execution of the 2 programs before the sequence, now this code execution, main thread main has been executed. For a property variable int b =100 There is no visibility problem with synchronized (and it is not necessary to use volatile declaration) when the 1 step is performed (thread t = new thread (TT); 1 The thread is a new state and has not yet begun to work. When the 2 steps are performed (T.start (); 2 When the Start method is called, this line Cheng is really started, into the runnable state, runnable state can be executed, everything is ready, but does not mean that the CPU must be executed on the above, there is no real implementation depends on the service CPU scheduling. The 3 step here must be to get the lock first (since start calls the native method, and it is ready after the completion, but does not mean that it must be done on the CPU, there is no real execution depending on the service CPU scheduling, then call the Run method, execute the M1 method). Here in fact, 2 synchronized methods inside the Thread.sheep in fact to do not matter, it is estimated that the confusion to increase the difficulty. When the 3 steps are executed, the child threads are also ready, but because of the presence of synchronized and the same object, the child thread must wait. Because the order of execution in the main method is sequential, it must be done in step 3 before you can go to 4 steps, and as the 3 step completes, the child thread can perform the M1. Here is a multithreaded who first gets to the question, if 4 steps first gets then main thread b=2000, if the child thread M1 obtains to possibly the B already assigns the value to 1000 or has not had the time to assign the value 4 steps to output the possible result is the main thread b= 1000 or main thread b=2000, where if the 6 steps are removed then b= execution in front and main thread b= is not sure. But because 6 steps exist, so no matter how all is main thread b= in front, then is equal to 1000 or 2000 see situation, after b=1000 is certain fixed.


Multithreading some suggestions


Thread is also very precious, so we recommend the use of thread pool, thread pool used a lot of follow-up ready to share, especially important, need to be aware of.

Give the thread a name, when the CPU high on the line, need to use the advanced jstack, if there is a very convenient name.

Multithreading specifically requires attention to thread safety issues, but also need to understand that the JDK are thread-safe and unsafe, so there will be no inexplicable problems when used.


There are some skills to share in the follow-up article slowly, multithreading is particularly important, but also particularly difficult, I hope that we also spend a lot of thoughts on the above.


Some debugging techniques for multiple threads


Because of breakpoints, all threads through the breakpoint, all need to stop, causing this point non-stop, very uncomfortable, eclispe inside conditional breakpoint, when meet the conditions can stop, then this is convenient.

-----------------------------------------------------------------------------------------------------------

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.