The variables within the 2.1.1 method are thread safe

Source: Internet
Author: User

A non-thread-safe problem exists in an instance variable, and if it is a private variable inside a method, there is no non-thread-safe problem, so it is thread-safe.

Test

1  PackageCom.cky.bean;2 3 /**4 * Created by Chenkaiyang on 2017/12/4.5  */6  Public classSelfprivatenum {7 8      Public voidAddi (String usrname) {9         Try {Ten             intNum =0; One             if(Usrname.equals ("a")) { ANum =100; -System.out.println ("A Over"); -Thread.Sleep (2000); the}Else { -Num= 200; -System.out.println ("B over"); -  +             } -System.out.println (usrname + "num=" +num); +}Catch(interruptedexception e) { A e.printstacktrace (); at         } -     } -}
1  PackageCom.cky.thread;2 3 ImportCom.cky.bean.SelfPrivateNum;4 5 /**6 * Created by Chenkaiyang on 2017/12/4.7  */8  Public classThreadaextendsthread{9     Privateselfprivatenum sn;Ten      PublicThreada (selfprivatenum sn) { One          This. SN =SN; A     } - @Override -      Public voidrun () { the         Super. Run (); -Sn.addi ("a"); -     } -}
1  PackageCom.cky.thread;2 3 ImportCom.cky.bean.SelfPrivateNum;4 5 /**6 * Created by Chenkaiyang on 2017/12/4.7  */8  Public classThreadbextendsthread{9     Privateselfprivatenum sn;Ten      Publicthreadb (selfprivatenum sn) { One          This. SN =SN; A     } - @Override -      Public voidrun () { the         Super. Run (); -Sn.addi ("B"); -     } -}
1  Packagecom.cky.test;2 3 ImportCom.cky.bean.SelfPrivateNum;4 ImportCom.cky.thread.ThreadA;5 ImportCom.cky.thread.ThreadB;6 7 /**8 * Created by Chenkaiyang on 2017/12/2.9  */Ten  Public classTest { One      Public Static voidMain (string[] args) { ASelfprivatenum sn =Newselfprivatenum (); -Threada Threada =NewThreada (SN); -THREADB threadb =Newthreadb (SN); the Threada.start (); - Threadb.start (); -     } -}
d:\it\jdk1.8\bin\java-didea.launcher.port=7532 "-didea.launcher.bin.path=d:\it\idea\intellij idea 2016.3.3\bin"- Dfile.encoding=utf-8-classpath "d:\it\jdk1.8\jre\lib\charsets.jar;d:\it\jdk1.8\jre\lib\deploy.jar;d:\it\jdk1.8\ Jre\lib\ext\access-bridge-64.jar;d:\it\jdk1.8\jre\lib\ext\cldrdata.jar;d:\it\jdk1.8\jre\lib\ext\dnsns.jar;d:\ It\jdk1.8\jre\lib\ext\jaccess.jar;d:\it\jdk1.8\jre\lib\ext\jfxrt.jar;d:\it\jdk1.8\jre\lib\ext\localedata.jar;d : \it\jdk1.8\jre\lib\ext\nashorn.jar;d:\it\jdk1.8\jre\lib\ext\sunec.jar;d:\it\jdk1.8\jre\lib\ext\sunjce_ Provider.jar;d:\it\jdk1.8\jre\lib\ext\sunmscapi.jar;d:\it\jdk1.8\jre\lib\ext\sunpkcs11.jar;d:\it\jdk1.8\jre\ Lib\ext\zipfs.jar;d:\it\jdk1.8\jre\lib\javaws.jar;d:\it\jdk1.8\jre\lib\jce.jar;d:\it\jdk1.8\jre\lib\jfr.jar;d : \it\jdk1.8\jre\lib\jfxswt.jar;d:\it\jdk1.8\jre\lib\jsse.jar;d:\it\jdk1.8\jre\lib\management-agent.jar;d:\it\ Jdk1.8\jre\lib\plugin.jar;d:\it\jdk1.8\jre\lib\resources.jar;d:\it\jdk1.8\jre\lib\rt.jar; F:\springboot\threaddemo\out\productiOn\threaddemo;d:\it\idea\intellij idea 2016.3.3\lib\idea_rt.jar " com.intellij.rt.execution.application.AppMain com.cky.test.Testa overb overb num=200a num=100

Results Analysis:

There is no non-thread-safe problem in the method variable, which is always thread-safe, which is caused by the method's internal variables being private.

The variables within the 2.1.1 method are thread safe

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.