2016-07-02
1 Synchronization Problem Java solution
Synchronization method: When the thread enters the synchronization method, it gets the lock of the object that the synchronization method belongs to.
Once an object lock is obtained, other threads can no longer execute any other synchronization methods of the locked object.
The lock is released only after the synchronization method has finished executing, and other threads can continue to execute.
Synchronized method declaration {
}
Sync Block:
Synchronized (Resource object) {
Methods that need to be synchronized
}
Packagecom.java1995;/*** Printer Class * *@authorAdministrator **/ Public classPrint {/** * * @paramName * Student name *@paramEscore * English score *@paramMscore * Math Results *@paramYscore * Language scores*/ Public voidPrintscore (String name,intEscore,intMscore,intYscore) {SYSTEM.OUT.PRINTLN (name+ "English score:" +Escore); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println (Name+ "Math Score:" +Mscore); SYSTEM.OUT.PRINTLN (Name+ "Language score:" +Yscore); }}
Packagecom.java1995;/*** Teacher class *@authorAdministrator **/ Public classTeacherImplementsrunnable{//Package PrivatePrint p; PrivateString name; Private intEscore,mscore,yscore; PublicTeacher (Print p,string name,intEscore,intMscore,intYscore) { This. p=p; This. name=name; This. escore=Escore; This. mscore=Mscore; This. yscore=Yscore; } //implementation of Encapsulation PublicPrint Getp () {returnp; } Public voidSetp (Print p) { This. P =p; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetescore () {returnEscore; } Public voidSetescore (intEscore) { This. Escore =Escore; } Public intGetmscore () {returnMscore; } Public voidSetmscore (intMscore) { This. Mscore =Mscore; } Public intGetyscore () {returnYscore; } Public voidSetyscore (intYscore) { This. Yscore =Yscore; } //override the Run () method@Override Public voidrun () {//TODO auto-generated Method Stub synchronized(P) {P.printscore (name, Escore, Mscore, Yscore); } } }
Packagecom.java1995; Public classTest { Public Static voidMain (string[] args) {Print P=NewPrint (); Teacher T1=NewTeacher (P, "Bolt", 10, 11, 12); Teacher T2=NewTeacher (P, "Gatling", 23, 56, 1); Teacher T3=NewTeacher (P, "featured", 34, 55, 90); Thread Th1=NewThread (t1); Thread Th2=NewThread (T2); Thread Th3=NewThread (T3); Th1.start (); Th2.start (); Th3.start (); }}
Packagecom.java1995; Public classSource {synchronized voidmethod1 () {System.out.println ("Enter the Method1 method, get the lock"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Method1 execution, Release lock"); } synchronized voidmethod2 () {System.out.println ("Enter the Method2 method, get the lock"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Method2 execution, Release lock"); }}
Packagecom.java1995; Public classMyThreadextendsthread{String name; Source s; MyThread (String name,source s) { This. name=name; This. s=s; } @Override Public voidrun () {//TODO auto-generated Method Stub if("Method1". Equals (name)) {s.method1 (); }Else{s.method2 (); } } Public Static voidMain (string[] args) {Source s=NewSource (); MyThread T1=NewMyThread ("Method1", s); MyThread T2=NewMyThread ("Method2", s); T1.start (); T2.start (); } }
Resources
[1] Java Easy Start Classic tutorial "full version"
Java 59th synchronization problem solution for Java