Multi-thread Resource Sharing
Keywords
Synchronized, sychronized block, concurrency, automic, mutex lock, volatile, lock scope, lock on Java object instance, and synchronized on Static Method
Lock syntax
The locks declared in the class object instance can be distinguished based on the modifier static from the locks owned by a single instance and the locks shared by multiple instances. synchronization is supported by using a synchronized marking method of the class, it has several different ways to select the most appropriate method in some specific scenarios, but it cannot blindly narrow down the scope of synchronized and increase the complexity of the code structure.
Only one thread of each class object instance can execute syncornized method.
Each class has only one thread and can execute synchronized static method.
public class Resource { private Object lockInst = new Object(); public void doWork() { synchronized(lockInst) { //do something } } public synchronized void doWorkSync() { //do something } public void doWorkSync2() { synchronized(this) { //do something } }}
public class Resource { private static Object lockInst = new Object(); public void doWork() { synchronized(lockInst) { //do something } }}
Synchronized on the Static Method
public class Resource {private static Object lockInst = new Object();public static void doWork() {synchronized(lockInst) {//do something}}public synchronized static void doWorkSync() {//do something}public static void doWorkSync2() {synchronized(Resource.class) {//do something}}}
Exercise
Package Org. ybygjy. thread3th. SYN;/*** multi-thread synchronization lock * <p> * 1. Create a multi-thread shared resource object, this object provides synchronized behavior * <p> * 2. creates multiple thread objects, all share public resources * @ author wangyancheng * @ version 2012-9-1 */public class synchronizedlocktest {/*** object instance-Level Lock */public static void dotest1 () {instancelock tminst = new instancelock (); For (INT I = 0; I <3; I ++) {New testthread (tminst ). start () ;}/ *** category lock */public static void dotest 2 () {for (INT I = 0; I <3; I ++) {New testthread (true ). start () ;}}/*** mixed use */public static void dotest3 () {instancelock tminst = new instancelock (); For (INT I = 0; I <3; I ++) {New testthread (tminst, true ). start () ;}/ *** test entry * @ Param ARGs parameter list */public static void main (string [] ARGs) {// synchronizedlocktest. dotest1 (); // synchronizedlocktest. dotest2 (); synchronizedlocktest. dotest3 (); While (thread. activecount ())! =-1) {try {thread. sleep (1);} catch (interruptedexception e) {e. printstacktrace ();}}}} /*** resource object for thread-sharing operations _ Lock with object instance level * @ author wangyancheng * @ version 2012-9-2 */class instancelock {/** object instance-level sharing */private object objkey = new object (); /** instance variable */private int counter; @ override Public String tostring () {return objkey. tostring ();}/*** a row is */Public void dowork () {synchronized (objkey ){ System. out. println (thread. currentthread (). getname () + "_" + (counter ++); If ("thread-0 ". equals (thread. currentthread (). getname () {try {thread. currentthread (). interrupt ();} catch (exception e) {e. printstacktrace ();}}}}} /*** resource object for thread-sharing operations _ Lock with object instance level * @ author wangyancheng * @ version 2012-9-2 */class classiclock {/** class-Level Lock */Private Static object objkey = new object (); /** class variable */private Static int counter;/*** action */public static void dowork () {synchronized (objkey) {system. out. println (thread. currentthread (). getname () + "_" + (counter ++); If ("thread-0 ". equals (thread. currentthread (). getname () {try {thread. currentthread (). interrupt ();} catch (exception e) {e. printstacktrace () ;}}}}/*** defines the Thread class for testing * @ author wangyancheng * @ version 2012-9-2 */class testthread extends Thread {/** multi-threaded instance sharing resources */private instancelock tminst;/** whether to share resources at the class level */private final Boolean isclassic; /*** constructor * @ Param isclassic true/false */Public testthread (Boolean isclassic) {This. isclassic = isclassic;}/*** constructor * @ Param tminst {@ link testthread} */Public testthread (instancelock tminst) {This. tminst = tminst; this. isclassic = false;}/*** constructor * @ Param TMI NST {@ link testthread} * @ Param isclassic true/false */Public testthread (instancelock tminst, Boolean isclassic) {This (isclassic); this. tminst = tminst;} @ override public void run () {While (! Isinterrupted () {If (! This. isclassic) {tminst. dowork ();} else if (null = tminst) {classiclock. dowork ();} else {classiclock. dowork (); tminst. dowork () ;}try {sleep (1000);} catch (interruptedexception e) {This. interrupt ();}}}}
References
- Java thread 3th
- Lock, http://en.wikipedia.org/wiki/Lock_ (computer_science)