[英]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. 我编写了一个简单的Thread程序以了解其工作原理。 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. 拉杰什。
You have created two different instances of Threading_Sample object. 您已经创建了Threading_Sample对象的两个不同实例。 Each instance will have its own copy of ArrayList object list1 每个实例将拥有其自己的ArrayList对象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 如果您需要两个线程都可以看到的数据列表,则需要将ArrayList传递给线程
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.