Java multi-thread synchronized Application

Source: Internet
Author: User

Http://hi.baidu.com/chaletli/item/beb128f39dccd413d7ff8cf5

 

Note the following when using the synchronized keyword:

1. the synchronized keyword cannot be inherited.

Although synchronized can be used to define a method, synchronized is not part of the method definition. Therefore, the synchronized keyword cannot be inherited. If a method in the parent class uses the synchronized keyword and overrides this method in the subclass, this method in the subclass is not synchronized by default, the synchronized keyword must be explicitly added to the subclass method. Of course, you can also call the corresponding methods in the parent class in the subclass method. Although the methods in the subclass are not synchronized, The subclass calls the synchronous method of the parent class. Therefore, the subclass method is equivalent to synchronization. The sample code for the two methods is as follows:

Add the synchronized keyword to the subclass method.

Class Parent
{
Public synchronized void method (){}
}
Class Child extends parent
{
Public synchronized void method (){}
}

Call the synchronization method of the parent class in the subclass Method

Class Parent
{
Public synchronized void method (){}
}
Class Child extends parent
{
Public void method () {super. Method ();}
}

2. You cannot use the synchronized keyword when defining interface methods.

3. the constructor cannot use the synchronized keyword, but you can use the synchronized block to be discussed in the next section for synchronization.

4. Synchronized can be freely placed.

In the previous example, the synchronized keyword is placed before the return type of the method. However, this is not the only location where synchronized can be placed. In a non-static method, synchronized can also be placed at the beginning of the method definition. In a static method, synchronized can be placed before static. The Code is as follows:

Public synchronized void method ();
Synchronized public void method ();
Public static synchronized void method ();
Public synchronized static void method ();
Synchronized public static void method ();

Note that synchronized cannot be placed behind the return type of the method. The following code is incorrect:

Public void Synchronized Method ();
Public static void Synchronized Method ();

The synchronized keyword can only be used for Synchronous methods and cannot be used to synchronize class variables. The following code is also incorrect.

Public synchronized int n = 0;
Public static synchronized int n = 0;

Although synchronized is the most secure synchronization method, a large number of synchronized keywords may cause unnecessary resource consumption and performance loss. Synchronized locks a method, but synchronized actually locks a class. That is to say, if synchronized is used for defining non-static methods Method1 and method2, method2 cannot be executed before Method1 is executed completely. Static and non-static methods are similar. However, static and non-static methods do not affect each other. Take a look at the following code:

Package test;
Public class mythread1 extends thread
{
Public String methodname;
Public static void method (string S)
{
System. Out. println (s );
While (true)
;
}
Public synchronized void Method1 ()
{
Method ("non-static Method1 method ");
}
Public synchronized void method2 ()
{
Method ("non-static method2 method ");
}
Public static synchronized void method3 ()
{
Method ("static method3 method ");
}
Public static synchronized void method4 ()
{
Method ("static method4 method ");
}
Public void run ()
{
Try
{
Getclass (). getmethod (methodname). Invoke (this );
}
Catch (exception E)
{
}
}
Public static void main (string [] ARGs) throws exception
{
Mythread1 mythread1 = new mythread1 ();
For (INT I = 1; I <= 4; I ++)
{
Mythread1.methodname = "method" + String. valueof (I );
New thread (mythread1). Start ();
Sleep (100 );
}
}
}

The running result is as follows:

Non-static Method1 Method

Static method3 Method

From the preceding running results, we can see that method2 and method4 cannot be run before Method1 and method3 are finished. Therefore, we can draw a conclusion that if the synchronized keyword is used in the class to define non-static methods, it will affect all non-static methods defined using the synchronized keyword. If static methods are defined, all static methods defined using the synchronized keyword in the class will be affected. This is a bit like a table lock in a data table. When a record is modified, the system locks the entire table. Therefore, using this synchronization method in large quantities will greatly degrade the program performance.

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.