"Go" Java multithreading (four) multi-threaded access to member variables and local variables

Source: Internet
Author: User
Tags java se

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

Let's look at a program example:

  

public class hellothreadtest{public    static void Main (string[] args)    {        Hellothread r = new Hellothread (); 
   thread T1 = new Thread (r);        Thread t2 = new Thread (r);        T1.start ();        T2.start ();    }} Class Hellothread implements runnable{    int i;    @Override public    Void Run ()    {        while (true)        {            System.out.println ("Hello number:" + i++);            Try            {                thread.sleep ((Long) math.random () *);            }            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 class hellothreadtest{public    static void Main (string[] args)    {        Hellothread r = new Hellothread (); 
   thread T1 = new Thread (r);        Thread t2 = new Thread (r);        T1.start ();        T2.start ();    }} Class Hellothread implements 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    @Override public    void Run ()    {        int i = 0;        Each thread will have its own copy of the local variable//The        thread does not affect        each other//so it prints 100 numbers, 0 to 49 each number is two times while        (true)        {            System.out.println ("Hello number:" + i++);            Try            {                thread.sleep ((Long) math.random () *);            }            catch (interruptedexception e)            {                e.printstacktrace ();            }            if (= = i)            {break                ;}}}    }

As noted in the note, because local variables have their own copy of the shell for each thread , the same variable is no longer shared between the threads, the output is 100 numbers, actually two groups, each group is 0 to 49 50 digits, 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.

"Go" Java multithreading (four) multi-threaded access to member variables and local variables

Related Article

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.