Thread synchronization
1, the purpose of thread synchronization is to protect multiple threads to access a resource when the resource is destroyed. 2, thread synchronization method is implemented by the lock, each object has a cut only a lock, the lock with a specific object, the thread once the object lock, the other access to the object's thread can no longer access the object's other synchronization methods. two ways to implement a synchronization mechanism1. Synchronizing code blocks:
synchronized (same data) {} The same data: N threads access one data at the same time.
2.
Synchronization method:
Public synchronized Data return type method name () {}
is to use synchronized to decorate a method, the method is called a synchronous method. For a synchronous method, there is no need to display the specified synchronization monitor, the synchronization monitor of the synchronous method is this that is the object itself (in this case the object itself is a bit vague, it is actually called the object of the synchronization method) by using the synchronous method, it is very convenient to turn a class into a thread-safe class, with the following characteristics:
1, objects of this class can be accessed by multiple threads safely.
2, after each thread invokes any method of the object, it will get the correct result.
3, after each thread invokes any method of the object, the object's state remains in a reasonable state.
Note: The Synchronized keyword can be used to modify a method, or to decorate a block of code, but not to decorate constructors, properties, and so on.
realize the synchronization mechanism note the following points: High security, low performance, in multi-threaded use. High performance, low security, in single-threaded use. The key code is as follows:writing a printer class: Printer defines two methods
Package Cn.d.happy;public class Printer { Object O=new object (); or before void plus synchronized public void Print1 () { ///Synchronous code block synchronized (o) { System.out.print ("line"); System.out.print ("Cheng"); System.out.print ("Same"); System.out.print ("step"); System.out.println (); } } public void Print2 () { synchronized (o) { System.out.print ("Oh"); System.out.print ("Ah"); System.out.println ();}}}
Define two thread classes and override the Run method. Inherits the thread and implements the Runnable interface through the For loop traversal count
Package Cn.d.happy;public class MyThread extends thread{public Printer print; @Overridepublic void Run () { //must have an object instance of the class for (int i = 1; I <=10; i++) { print.print1 () }}}
Package Cn.d.happy;public class MyThread2 implements runnable{ public Printer print; @Override public Void Run () {for (int i = 1; I <=10; i++) { print.print2 ();}} }
The test class creates a printer object and two thread objects and assigns values
Package Cn.d.happy;public class Test {public static void main (string[] args) { //purchase a printer Printer p=new Printer (); //Create first thread object and assign value to attribute MyThread t1=new MyThread (); t1.print=p; T1.start (); 03. Create a second thread object and assign a value to the attribute MyThread2 t2=new MyThread2 (); t2.print=p; Thread tt=new thread (t2); Tt.start ();}}
Implementation results:
Java thread Synchronization