两个类分别如下:
- <pre name="code" class="java">package Demo2;
- import java.util.*;
- class Demo2
- {
- private static ArrayList<String> a = new ArrayList<String>();
- public static void main(String[] args)
- {
- a.add("String0");
- threadDemo2 thread1 = new threadDemo2(a);
- new Thread(thread1).start();
- for(int i = 0;i<500; i++){
- for(int j = 0;j<500; j++)
- ;
- }
- for(String string : a)
- {
- sop(string);
- }
- }
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- }
- package Demo2;
- import java.util.*;
- class threadDemo2 implements Runnable
- {
- private ArrayList<String> b;
- threadDemo2(ArrayList<String> a){
- this.b = a;
- }
- public void run()
- {
- b.add("String");
- }
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- }
主函数中输出结果为:
String0
String
也就是在子线程中获得了ArrayList a 的地址,并对地址内的值进行了操作。
原文地址:https://www.cnblogs.com/jpfss/p/10784029.html
时间: 2024-10-14 04:18:59