Example variables and thread safety for Java Learning notes

Source: Internet
Author: User

instance variables and thread safety:

Do not share data:

 Public classNosharedthreadextendsThread {Private intCount = 5;  PublicNosharedthread (String name) {Super();  This. SetName (name); } @Override Public voidrun () {Super. Run ();  while(Count > 0) {Count--; System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + ", Count:" +count); }    }} Public classThreadrunmain { Public Static voidMain (string[] args) {testsharedsafethead (); }     Public Static voidTestnosharedthread () {Nosharedthread a=NewNosharedthread ("a"); Nosharedthread b=NewNosharedthread ("B"); Nosharedthread C=NewNosharedthread ("C");        A.start ();        B.start ();    C.start (); }}

Operation Result:

The visible threads do not interfere with each other, and are manipulating their own count variables.

Sharing data:

 Public classSharedthreadextendsthread{Private intCount = 5;  PublicSharedthread (String name) {Super();  This. SetName (name); } @Override Public voidrun () {Super. Run (); Count--; System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + ", Count:" +count); }} Public classThreadrunmain { Public Static voidMain (string[] args) {testsharedthead (); } Public Static voidTestsharedthead () {Sharedthread St=NewSharedthread ("Sharedthread"); Thread a=NewThread (St, "a"); Thread b=NewThread (St, "B"); Thread C=NewThread (St, "C"); Thread D=NewThread (St, "D"); Thread e=NewThread (St, "E");        A.start ();        B.start ();        C.start ();        D.start (); E.start ();}}

Operation Result:

A non-thread-safe problem occurs when the visible threads interfere with each other, producing the same count value.

Shared Data thread Safety:

 Public classSharedsafethreadextendsthread{Private intCount = 5;  PublicSharedsafethread (String name) {Super();  This. SetName (name); } @Override Public synchronizedvoidrun () {Super. Run (); Count--; System.out.println ("Thread Name:" + Thread.CurrentThread (). GetName () + ", Count:" +count); }} Public classThreadrunmain { Public Static voidMain (string[] args) {testsharedsafethead (); }     Public Static voidTestsharedsafethead () {Sharedsafethread SST=NewSharedsafethread ("Sharedsafethread"); Thread a=NewThread (SST, "a"); Thread b=NewThread (SST, "B"); Thread C=NewThread (SST, "C"); Thread D=NewThread (SST, "D"); Thread e=NewThread (SST, "E");        A.start ();        B.start ();        C.start ();        D.start ();    E.start (); }}

Operation Result:

The visible threads do not interfere with each other, and the count value is not duplicated and thread safe.

Example variables and thread safety for Java Learning notes

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.