Java Multithreading (quad) multi-threaded access to member variables and local variables

Source: Internet
Author: User

Java Multithreading (quad) multi-threaded access to member variables and local variables

Let's look at a program example:

  

Public classhellothreadtest{Public static voidMain (string[] args) {Hellothread r =NewHellothread (); Thread T1 =NewThread (R); Thread t2 =NewThread (R);        T1.start ();    T2.start (); }}classHellothreadimplements Runnable{intI @OverridePublic voidRun () { while(true) {System. out. println ("Hello number:" + i++);Try{Thread.Sleep (Long) Math.random () * 1000); }Catch(Interruptedexception e)            {E.printstacktrace (); }if(= = i) { Break; }        }    }}

In this example, the Hellothread class implements the Runnable interface, where the main work of the run () method is to output the "Hello number:" String plus the digit I, and increment I at the same time, when I arrives 50 o'clock, exits the loop.

The main () method generates an object R for the Hellothread class and generates two threads from that object .

The result of the execution of the program is that 0 to 49 digits are printed sequentially, with a total of 50 digits.

This is becausei is a member variable , then the Hellothread object R contains only this one I, and the two thread objects are shared with the same I because it is constructed by R.

When we change the code as follows (the original member variable i is commented out, the local variable I in the method is added):

Public classhellothreadtest{Public static voidMain (string[] args) {Hellothread r =NewHellothread (); Thread T1 =NewThread (R); Thread t2 =NewThread (R);        T1.start ();    T2.start (); }}classHellothreadimplements Runnable{//int i; If I is a member variable, then the Hellothread object R contains only this one I, two thread objects because of the r constructs, so shared the same I//print result is 0 to 49 of the number @OverridePublic voidRun () {inti = 0; Each thread will have its own copy of the local variable//threads do not affect each other//So print 100 numbers, 0 to 49 each number is two times while(true) {System. out. println ("Hello number:" + i++);Try{Thread.Sleep (Long) Math.random () * 1000); }Catch(Interruptedexception e)            {E.printstacktrace (); }if(= = i) { Break; }        }    }}

As noted in the note, because local variables have their own copies for each thread , the same variable is no longer shared between the threads, and the output is 100 numbers, actually two groups, each of which is 50 numbers from 0 to 49. And the two sets of numbers are randomly interspersed.

The conclusions were as follows:

  If a variable is a member variable, when multiple threads manipulate member variables of the same object , they affect each other on that member variable, meaning that a thread changes the member variable to affect another thread.

  If a variable is a local variable , then each thread will have a copy of that local variable (that is, the local variable of the method in the same object, and one copy for each thread), and the change to that local variable by one thread does not affect other threads.

Resources

Santhiya Garden Zhang Long teacher Java SE Series video tutorial.

Source: http://www.cnblogs.com/mengdd/archive/2013/02/16/2913659.html

Java Multithreading (quad) multi-threaded access to member variables and local variables

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.