Java multithreaded programming: variable sharing analysis (Thread)

Source: Internet
Author: User

The most important thing to do when writing multithreaded programming is to figure out which variables are shared and which are not. That is to analyze the principle of it.

Since you have recently used multithreading to see some of the threads that are created with subclasses of the thread class , it is summarized as follows:

1. The local variables defined inside the method body are not shared

This is because the variables defined inside the method are dynamically generated at run time . Each thread has its own stack, which is used to hold the data at run time.

The easiest to understand is the recursive call time, each time into the stack operation. as follows, the variable AA is saved on the run-time stack each time it is called, and the method end variable is freed.

public int fib (int n) {    int aa;    if (n==1 | | n==0)        return 1;    else        return fib (n-1) *n;}

2. Member variables

2.1 Code Examples

Member variables need to see if the variable points to the same object . Look at the following code example:

Package File2;public class Analy {public    static void Main (string[] args) {        num i=new num (0);    New object, ready to be passed to thread        new Ownthread (i). Start ();    Create a new thread and start        new Ownthread (i). Start ();    Create a new thread and start        System.out.println (the value of I in the main thread becomes: "+i.i);    Gets the current value of object I    }}class ownthread extends thread{    Num ID;    Declaration object, default null, is not pointing to any entity    int sno;    declares an int variable. Because the system is initialized to 0 by default, you should define an int variable    ownthread (Num id)    {        this.id=id;    }        public void Run ()    {-         (int i=0;i<5;i++)        {            synchronized (this)            {                sno=id.i;    Save the value of ID.I to the thread private variable sno                id.i++;                try {                    thread.sleep (1);                }                 catch (Interruptedexception e) {}            }            System.out.println (This.getname () + "," +sno);        }    }} Class Num    //defines a category {    int i;    Num (int i)    {        this.i=i;    }}

To share the same object, the thread can interact and execute the result:

2.2 Analysis

The main function in the program defines the instance I of the Num object, which defines that the thread is an instance of a num object that is shared with three variables such as Thread0 and Thread1. Thread Thread0 and thread Thread1 have their own private variable sno, which can be used to hold the value of a shared variable for a moment.

Note: (1) in Java, the object is judged by the same object using the address . The same address is the same object, the above three is the same object.

(2) It is not possible to replace an object instance shared in the above example with a basic data type. Because the basic data type program is automatically initialized with default values, which are declared and defined together. At this point the thread is defined in the Mian function, and the basic data type parameter passed is only another object in the initialization thread, not the same object.

3. Summary

In short, in multithreaded programming, it is important to know how and how each thread shares the data.

As in the above program, you can share an object between the main thread and the other two sub-threads to achieve interactive processing between them.

Java multithreaded programming: variable sharing analysis (Thread)

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.