Rajesh Subramanian
Rajesh Subramanian

Reputation: 171

When one thread modifies the global variable, is it visible to the other thread also?

I wrote a simple Thread program to understand how it works. The problem is that the two threads seem to have their own copy of a single Global variable. How can i re-write my code so that when one thread modifies the global variable, it is visible to the other thread also ?

import java.io.*;
import java.lang.*;
import java.util.*;

public class Threading_Sample implements Runnable
{
    private Thread T1,T2;
    String ThreadName="";

    ArrayList<String> List1=new ArrayList<String>();

    Threading_Sample(String name)
    {
        ThreadName=name;
    }

    public void run()
    {
        while(true)
        {
            try
            {
                if(ThreadName.equals("THREAD1"))
                {
                    System.out.println("Current Thread: "+ThreadName);
                    System.out.println("DOING TASK 1...");

                    List1.add("A");
                    System.out.println("List1 of Thread1: "+List1);
                }
                if(ThreadName.equals("THREAD2"))
                {
                    System.out.println("Current Thread: "+ThreadName);
                    System.out.println("DOING TASK 2...");

                    List1.add("C");
                    System.out.println("List1 of Thread2: "+List1);
                }
                Thread.sleep(100);
            }
            catch(Exception e)
            {
                System.out.println("Thread interrupted...");
            }

        }

    }

    public void start()
    {
        if(T1==null)
        {
            T1=new Thread(this,ThreadName);
            T1.start();
        }
        if(T2==null)
        {
            T2=new Thread(this,ThreadName);
            T2.start();
        }
    }

    public static void main (String[] args)
    {
        Threading_Sample TASK1=new Threading_Sample("THREAD1");
        Threading_Sample TASK2=new Threading_Sample("THREAD2");     

        TASK1.start();
        TASK2.start();
    }
}

Output:

Current Thread: THREAD1    
DOING TASK 1...    
Current Thread: THREAD1    
DOING TASK 1...    
List1 of Thread1: [A, A]    
List1 of Thread1: [A, A]    
Current Thread: THREAD2    
DOING TASK 2...    
List1 of Thread2: [C]    
Current Thread: THREAD2    
DOING TASK 2...    
List1 of Thread2: [C, C]    
Current Thread: THREAD1    
DOING TASK 1...   
Current Thread: THREAD2    
DOING TASK 2...    
Current Thread: THREAD1    
DOING TASK 1...    
List1 of Thread2: [C, C, C]    
List1 of Thread1: [A, A, A]    
List1 of Thread1: [A, A, A, A]    
Current Thread: THREAD2    
DOING TASK 2...    
List1 of Thread2: [C, C, C, C]    
Current Thread: THREAD1    
DOING TASK 1...    
Current Thread: THREAD2   
DOING TASK 2...    
List1 of Thread1: [A, A, A, A, A]   
List1 of Thread2: [C, C, C, C, C]    
Current Thread: THREAD2    
DOING TASK 2...    
Current Thread: THREAD1    
DOING TASK 1...    
List1 of Thread2: [C, C, C, C, C, C]    
List1 of Thread1: [A, A, A, A, A, A]    
Current Thread: THREAD2    
DOING TASK 2...    
Current Thread: THREAD1    
DOING TASK 1...    
List1 of Thread2: [C, C, C, C, C, C, C]    
List1 of Thread1: [A, A, A, A, A, A, A]

Regards,

Rajesh.

Upvotes: 0

Views: 533

Answers (1)

vinayknl
vinayknl

Reputation: 1252

You have created two different instances of Threading_Sample object. Each instance will have its own copy of ArrayList object list1

 ArrayList<String> List1=new ArrayList<String>();

So, there are two different objects which are modified by respective threads. If you need a list of data which is to be visible to both the threads, then you need to pass an ArrayList to your threads

 public class Threading_Sample implements Runnable
 {
    private ArrayList<String> list1;
    ....
    Threading_Sample(String name, ArrayList<String> list1)
    {
        ThreadName=name;
        this.list1 = list1;
    }
  ... 

public static void main (String[] args)
{
     ArrayList<String> List1=new ArrayList<String>();


    Threading_Sample TASK1=new Threading_Sample("THREAD1",list1);
    Threading_Sample TASK2=new Threading_Sample("THREAD2",list1);     

    TASK1.start();
    TASK2.start();
}

Upvotes: 3

Related Questions