Java thread synchronization essentially conforms to a logic: lock------> Modify------> Release lock
1. Sync code block
Examples are as follows:
public class SyncBlock {
static class Datawrap {
int i;
}
Static Class Syncblockthread extends Thread {
private datawrap date;
Public Syncblockthread (Datawrap datawrap) {
this.date = datawrap;
}
@Override public
Void Run () {(
int i = 0; i < i++) {
synchronized (date) {
date.i++
; try {sleep
(1);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
System.out.println (GetName () + "" + date.i);
}
}}} public static void Main (string[] args) {
//Multithreading implementation variable I in turn add one output
datawrap datawrap = new Datawrap ();
New Syncblockthread (Datawrap). Start ();
New Syncblockthread (Datawrap). Start ();
New Syncblockthread (Datawrap). Start ();
}
}
In the example, you want to output integers sequentially.
Typically, a synchronized code block is an object that needs to be locked, typically a shared resource that requires concurrent access, and any thread will first lock the resource before modifying the specified resource, and other threads cannot modify the resource during the lock. Thus ensuring the security of the thread. Another thread does not yield a resource lock when calling sleep or yield.
2. Synchronous method
public class Syncmethod {static class datawrap{int i;
Public synchronized void Valuegrow () {i++;
try {thread.sleep (1);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
} System.out.println (Thread.CurrentThread (). GetName () + "" + i);
} static class Syncmethodthread extends Thread {datawrap datawrap;
Public Syncmethodthread (Datawrap datawrap) {this.datawrap = Datawrap;
@Override public void Run () {for (int i = 0; i < i++) {datawrap.valuegrow (); }} public static void Main (string[] args) {//implementation order growth and output I datawrap datawrap in datawrap = new Da
Tawrap ();
New Syncmethodthread (Datawrap). Start ();
New Syncmethodthread (Datawrap). Start ();
New Syncmethodthread (Datawrap). Start (); }
}
The synchronization method is a method that is decorated with the Synchronized keyword. The synchronization method locks the object itself, so when a thread calls a synchronization method for an object, if another thread calls another synchronization method for that object, it still waits to release the lock on the object because the object is locked.
3. Synchronous lock
Synchronization is achieved by defining a synchronization lock object, in which case the lock object is used as the synchronization latch.
Import Java.util.concurrent.locks.Lock;
Import Java.util.concurrent.locks.ReentrantLock;
public class SyncLock {static class datawrap{lock lock = new Reentrantlock ();
int i;
public void Valuegrow () {lock.lock ();
try {i++;
try {thread.sleep (1);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
} System.out.println (Thread.CurrentThread (). GetName () + "" + i);
finally {Lock.unlock ();
}} static Class Synclockthread extends Thread {datawrap datawrap;
Public Synclockthread (Datawrap datawrap) {this.datawrap = Datawrap;
@Override public void Run () {for (int i = 0; i < i++) {datawrap.valuegrow (); }} public static void Main (string[] args) {//implementation order growth and output I datawrap datawrap in datawrap = new Da
Tawrap ();
New Synclockthread (Datawrap). Start ();
New Synclockthread (Datawrap). Start ();
New Synclockthread (Datawrap). Start ();
}
}
It is more flexible to implement thread synchronization using lock objects, and some locks also have certain features, which are more commonly used readwritelock read and write locks, Reentrantlock can be reentrant.