The following code shows how to synchronize a specific class method:
Copy Code code as follows:
Package mythread;
public class Syncthread extends Thread
{
private static String sync = "";
Private String Methodtype = "";
private static void Method (String s)
{
Synchronized (sync)
{
Sync = s;
System.out.println (s);
while (true);
}
}
public void Method1 ()
{
Method ("Method1");
}
public static void StaticMethod1 ()
{
Method ("StaticMethod1");
}
public void Run ()
{
if (Methodtype.equals ("Static"))
StaticMethod1 ();
else if (methodtype.equals ("nonstatic"))
Method1 ();
}
Public Syncthread (String methodtype)
{
This.methodtype = Methodtype;
}
public static void Main (string[] args) throws Exception
{
Syncthread sample1 = new Syncthread ("nonstatic");
Syncthread sample2 = new Syncthread ("Static");
Sample1.start ();
Sample2.start ();
}
}
The results of the operation are as follows:
Copy Code code as follows:
Many readers may be surprised to see the results of the above run. In the above code, the Method1 and StaticMethod1 methods use the static string variable sync to synchronize. The two methods can have only one execution at a time, and both of these methods perform an infinite loop of 014 lines. As a result, the output can only be one of method1 and staticMethod1. But this program prints both of these strings.
The willingness to come up with this kind of result is simple, let's take a look at 012 lines to know. Originally in this line of the value of the sync changed. Here is the string type in Java. The string type differs from other complex types in Java. When you use a string variable, Java creates a new instance of string, whenever you assign a value to the variable. As shown in the following code:
Copy Code code as follows:
String s = "Hello";
System.out.println (S.hashcode ());
s = "World";
System.out.println (S.hashcode ());
In the code above. The value of the hashcode of the first S and the again assigned S is not the same. Because creating an instance of a string class does not require the use of new, you should be careful not to assign a value to a variable of type string, otherwise the variable will not be synchronized.
Since a new instance has been created for sync on line 012, assuming that method1 executes first, and when the Method1 method executes 013 lines of code, the sync value is not the original value, and the Method1 method locks the original value of the sync variable. And at this point, staticMethod1 exactly to synchronized (sync), in the StaticMethod1 method to lock the sync and method1 method locked sync is not a, therefore, The synchronization of the two methods has been compromised.
The solution to the above problem is of course to remove 012 lines. In this case, this line is added only to illustrate that the synchronization between methods is broken if the value of the synchronization variable is changed in the synchronized block when using the class variable to synchronize the method. To avoid this completely, you can use the final keyword when defining a synchronization variable. If you can change the 005 lines in the above program to the following form:
Copy Code code as follows:
Private final static String sync = "";
When
uses the final keyword, sync can only be assigned a value when defined, and can no longer be modified. If you assign a value to sync elsewhere in the program, the program cannot be compiled. In development tools such as Eclipse, you will be prompted directly in the wrong place.
We can understand synchronized blocks from two angles. If you understand from the perspective of a class method, you can synchronize the corresponding method with the class variable. If you understand from the perspective of a class variable, you can use a synchronized block to guarantee that a class variable can be accessed by only one method at a time. Regardless of the angle from which to understand, their essence is the same, that is, the use of class variables to obtain synchronous locks through the synchronization of the mutex to achieve synchronization.
Note: When using synchronized blocks, it should be noted that synchronized blocks can only use objects as its parameters. If you are a variable of a simple type (such as int, Char, Boolean, and so on), you cannot use synchronized to synchronize.