Java thread synchronization and java Thread Synchronization
Java thread synchronization basically needs to conform to a logic: locking ------> modifying ------> releasing the lock
1. synchronous code block
Example:
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 () {for (int I = 0; I <10; 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) {// multi-threaded implementation variable I add one to output DataWrap dataWrap = new DataWrap (); new SyncBlockThread (dataWrap ). start (); new SyncBlockThread (dataWrap ). start (); new SyncBlockThread (dataWrap ). start ();}}
In this example, you want to output integers in sequence.
A synchronization code block is usually an object to be locked. It is generally a shared resource that requires concurrent access. Any thread first locks the resource before modifying the specified resource, other threads cannot modify the resource during the lock process. This ensures thread security. In addition, when the thread calls sleep or yield, the resource lock is not allowed.
2. Synchronization 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 <10; I ++) {dataWrap. valueGrow () ;}} public static void main (String [] args) {// achieve sequential growth and output I Datawrap DataWrap = new dataWrap () in DataWrap (); new SyncMethodThread (dataWrap ). start (); new SyncMethodThread (dataWrap ). start (); new SyncMethodThread (dataWrap ). start ();}}
The synchronous method is a method modified using the synchronized keyword. The synchronization method is locking the object itself. Therefore, when a thread calls the synchronous method of an object, if other threads call other Synchronization Methods of the object, they still need to wait to release the lock of the object because the object has been locked.
3. Synchronization lock
Synchronization is implemented by defining the synchronization Lock object. In this case, the synchronization Lock uses the Lock Object to act.
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 <10; I ++) {dataWrap. valueGrow () ;}} public static void main (String [] args) {// achieve sequential growth and output I Datawrap DataWrap = new dataWrap () in DataWrap (); new SyncLockThread (dataWrap ). start (); new SyncLockThread (dataWrap ). start (); new SyncLockThread (dataWrap ). start ();}}
The use of lock objects to implement thread synchronization is more flexible. Some locks also have some specific functions. Among them, the ReadWriteLock read and write locks and ReentrantLock can be used to re-import locks.