When a thread enters a synchronized () method of an object, can other threads access other methods of this object?

Source: Internet
Author: User

Thinking...

Reference: http://hanhongke123.blog.163.com/blog/static/62223494201081392124586/

No. One Synchronized Method of an object can only be accessed by one thread.

 

Java synchronized usage Summary

1. When synchronized is used as a function modifier, the sample code is as follows:



Public synchronized void method (){

//....

}

This is the synchronization method. Which object is synchronized locked at this time? The lock is to call this synchronization method object. That is to say, when an object P1 executes this synchronization method in different threads, they form mutual exclusion to achieve synchronization. However, the other object P2 generated by the class to which this object belongs can call the method with the synchronized keyword.

The above sample code is equivalent to the following code:



Public void method ()

{

Synchronized (this) // (1)

{

//.....

}

}

(1) What does this mean? It refers to the object that calls this method, such as P1. It can be seen that the synchronization method essentially acts on the object reference. -- The thread that obtains the P1 object lock can call the synchronization method of P1. For P2, the P1 lock has nothing to do with it, in this case, the program may also get rid of the control of the synchronization mechanism, resulting in data confusion.

2. Synchronization block. The sample code is as follows:



Public void method (someobject so ){

Synchronized (SO)

{

//.....

}

}

In this case, the lock is the so object. Whoever gets the lock can run the code that he controls. When a specific object is used as a lock, the program can be written in this way, but when there is no clear object as a lock, just want to synchronize a piece of code, you can create a special instance variable (which must be an object) to act as a lock:



Class Foo implements runnable

{

Private byte [] Lock = new byte [0]; // special instance variable

Public void method ()

{

Synchronized (LOCK ){//... }

}

//.....

}

Note: creating a zero-Length byte array object is more economical than any other object-view the compiled bytecode: only three operation codes are required for generating a zero-Length byte [] object, object lock = new object () requires seven lines of operation code.

3. apply synchronized to the static function. The sample code is as follows:



Class foo

{

Public synchronized static void Method1 () // synchronized static function

{

//....

}

Public void method2 ()

{

Synchronized (FOO. Class) // class literal (Class Name literal constant)

}

}



The method2 () method in the Code uses class literal as the lock. It produces the same effect as the synchronized static function, and the obtained lock is very special, is the class of the object currently calling this method (class, instead of a specific object generated by this class ).

I remember that I saw in objective Java that Foo. Class and p1.getclass () are used for synchronization locks. p1.getclass () cannot be used to lock this class. P1 refers to the object generated by the foo class.

It can be inferred that a class defines a static function a of synchronized, and also defines the instance function B of synchronized, when the same object OBJ of this class accesses Methods A and B in multiple threads, the synchronization will not be formed because their locks are different. The lock of method A is the class to which OBJ belongs, and the lock of Method B is the object to which OBJ belongs.

Java synchronized usage is summarized as follows:

Figuring out which object synchronized is locked can help us design safer multi-threaded programs.

There are also some tips to make our synchronized access to shared resources more secure:

1. Define the private instance variable + its get method, instead of the public/protected instance variable. If the variable is defined as public, the object can bypass the control of the synchronization method and directly obtain and change it. This is also one of the standard implementations of JavaBean.

2. if the instance variable is an object, such as an array or an arraylist, the above method is still insecure, because after the External Object obtains the reference of this instance object through the get method, and direct it to another object, so this private variable also changes, it is not very dangerous. At this time, you need to add the get method with synchronized synchronization, and only return the clone () of this private object-in this way, the caller obtains the reference of the object copy.

******************

The wait () method is a synchronous control method provided by the java. Lang. Object Class Wei thread to implement inter-thread communication. The wait () method enables the current thread to actively release the mutex lock and enter the wait queue for the mutex lock. (That is, it suspends the current thread and waits for other threads to execute the Y () or notifyall () method before continuing to execute this thread .) This method is used to release the thread of a project and wake up another thread that may be waiting. There are two call formats:

1. Wait () waits for the communication thread to wake up before continuing to execute this thread.

2. Wait (long millis) waits for the communication thread to wake up or wait for a maximum of millis milliseconds before continuing to execute this thread.

The wait () method can only be placed in the synchronization method or block, indicating that the thread needs to wait for resource synchronization.

The sleep () method can be placed anywhere, indicating that the current thread is sleeping.

The wait () method releases the object lock. The sleep () method does not release the object lock.

The wait () method will not be executed until it is awakened.

Sleep () is sleep for a period of time, and the thread automatically resumes execution.

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.