Java section 58th the origin of thread synchronization issues

Source: Internet
Author: User

2016-07-01

 Packagecom.java1995;/*** Printer class *@authorAdministrator **/ Public classPrint {/**     *      * @paramName Student Name *@paramescore English Results *@paramMscore Math Results *@paramYscore Language Achievement*/     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 StubP.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 (); }}

Java section 58th the origin of thread synchronization issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.