We can useSynchronizedBlock to synchronize specific static or non-static methods. To implement this requirement, you must define a class variable for the methods of these features, and then use the code of these methodsSynchronizedBlock, and pass this class variable as a parameterSynchronizedBlock. The following codeDemonstrate how to synchronize specific class methods:
001 Package Mythread;
002
003 Public Class SyncThread Extends Thread
004 {
005 Private Static String sync = "" ;
006 Private String methodType = "" ;
007
008 Private Static Void Method (String s)
009 {
010 Synchronized (Sync)
011 {
012 Sync = S;
013 System. out. println (s );
014 While ( True );
015 }
016 }
017 Public Void Method1 ()
018 {
019 Method ( " Method1 " );
020 }
021 Public Static Void StaticMethod1 ()
022 {
023 Method ( " StaticMethod1 " );
024 }
025 Public Void Run ()
026 {
027 If (MethodType. equals ( " Static " ))
028 StaticMethod1 ();
029 Else If (MethodType. equals ( " Nonstatic "