1. Synchronizing with class objects
When two threads access the same class object, there is a competition. Synchronizing locks are objects, not code.
Package Thrds;public class Fivethread {public static void main (String args[]) {thtst obj = new thtst (); thread T1 = new Thread (obj); Two threads with the same object, a mutex (belonging to the object mutex) thread t2 = new Thread (obj); T1.start (); T2.start ();}} Class Thtst extends Thread {public void run () {TST ();} synchronized void TSt () {System.out.println (Thread.CurrentThread (). GetId () + "in"); try {thread.sleep ()} catch ( Interruptedexception e) {e.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetId () + "Out");}} Output: In15 out14 in14 out if you change the main function above to: thread T1 = new Thread (new THTST ()); With two objects, there will be no competition for thread t2 = new Thread (new THTST ()); output: In15 in15 out17 out
2. Use class definitions and objects for synchronization (class. Class)
A competition can occur even if two threads access different objects individually. The lock is a code snippet.
Because the class definition of a class has only one, there can only be one thread in the lock where it executes.
Note: null cannot be used as a synchronization object.
Package Thrds;public class Sixthread {public static void main (String args[]) {Thread T1 = new Thread (new ThTst2 ()); Thread t2 = new Thread (new ThTst2 ()); T1.start (); T2.start ();}} Class ThTst2 extends Thread {private static Integer IOBJ = 9;public void Run () {TST ();} synchronized void TSt () {synchronized (Thtst2.class) {//is available here for any class type, such as String.class, Integer.class, or using other objects such as Iobj The same is true of System.out.println (Thread.CurrentThread (). GetId () + "in"), try {thread.sleep ();} catch ( Interruptedexception e) {e.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetId () + "Out");}} Results: In14 out16 In16 out
3.2 uses object synchronization, which is used to synchronize objects that can be passed in by parameters. This gives you more flexibility and allows programmers to control which threads compete for the same object. (Can be referenced at design time)
Package Thrds;import Org.omg.corba.inv_objref;public class Seventhread {public static void main (String args[]) {THTST3 TObj1 = new ThTst3 (); ThTst3 tObj2 = new ThTst3 (); String lock = new String ("lock"); Tobj1.setlock (lock); Tobj2.setlock (lock); thread T1 = new Thread (TOBJ1); Thread t2 = new Thread (TOBJ2); T1.start (); T2.start ();}} Class ThTst3 extends Thread {private String lock = null, @Overridepublic void Run () {TST ();} synchronized void TSt () {synchronized (lock) {///sync objects are passed into the System.out.println (Thread.CurrentThread (). GetId () + "in"); try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetId () + "Out");}} void Setlock (String lock) {this.lock = lock;}} Results: In15 out16 In16 out
Synchronized keyword, multithreading when synchronizing, you need to look at the objects used to synchronize (including the class definition) is not unique. Mutual exclusion occurs only when the object is unique.
Java multithreaded synchronization –synchronized usage