【问题标题】:Java - Using a static list from another classJava - 使用另一个类的静态列表
【发布时间】:2013-08-31 13:10:24
【问题描述】:

我有一个这样的类,我在其中更新一个线程中的静态变量。我需要从另一个类访问这个变量。

import java.util.ArrayList;
import java.util.List;

public class VariableUpdater implements Runnable {
    static List < String > abc = new ArrayList < String > ();

    private static VariableUpdater instance = null;

    private VariableUpdater() {}

    public static synchronized VariableUpdater getInstance() {
        if (instance == null) {
            instance = new VariableUpdater();
        }
        return instance;
    }

    public static void main(String[] args) {
        Thread th = new Thread( VariableUpdater.getInstance());
        th.start();
    }

    @Override
    public void run() {
        while (true) {
            System.out.println();
            try {
                abc.add("aa");
                Thread.sleep(1000);
                printContent();
            } catch (Exception e) {
                // TODO: handle exception
            }

        }

    }

    public synchronized void printContent() {
        for (String string: abc) {
            System.out.println(string);
        }
    }
}

并且这个变量需要像这样从另一个类访问:

public class Accessor {
    public static void main(String[] args) {
        VariableUpdater.getInstance().printContent();
    }
}

问题是,当运行 Accessor 类时,列表是空的。

我错过了什么吗?

更新/解决方案

事实证明,我们可以通过使用 Hazelcast 或某种消息传递/缓存实用程序来实现这一点。我会尽快发布完整的解决方案。

来源:How to share object between java applications?

【问题讨论】:

  • 你有两个main 方法。假设你开始最后一个:你什么时候开始你的线程?
  • 我先运行 VariableUpdater 类。逻辑每隔一秒更新一次变量。并且列表需要从 Accessor 类中访问。
  • 你必须有两个 JVM 实例。检查我的答案。

标签: java singleton static-variables


【解决方案1】:

从这段代码你可以访问另一个类对象中的列表

import java.util.ArrayList;
import java.util.List;

 public class VariableUpdater implements Runnable {
 static List < String > abc = new ArrayList < String > ();

private static VariableUpdater instance = null;

private VariableUpdater() {}

public static synchronized VariableUpdater getInstance() {
    if (instance == null) {
        instance = new VariableUpdater();
    }
    return instance;
}

public static void main(String[] args) {
    Thread th = new Thread(new VariableUpdater());
    th.start();
    Accessor.print();
}

@Override
public void run() {
   for(int i=0;i<10;i++) {
        System.out.println();
        try {
            abc.add("aa");
           // Thread.sleep(1000);
            //printContent();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

public synchronized void printContent() {
        System.out.println("List :: " + abc);
}
 }

class Accessor {
public static void print() {
    System.out.println("Accessor");
    VariableUpdater.getInstance().printContent();
  }
}

【讨论】:

  • 那不是我想要的。我需要在后台运行 VariableUpdater 类,并且需要从另一个偶尔运行的类访问列表。
【解决方案2】:

您在两个不同的类中有两个 main() 方法。在运行两个main() 方法时,将有两个JVM 实例,它们不共享任何东西。所以你的列表永远是空的。

使用一种main() 方法来启动线程。

public class Main{

    //shared state
    public static void main(String[] args){

        VariableUpdator variableUpdatorInstance = ...
        Accessor accessorInstance = ...


        variableUpdatorInstance.start();
        accessorInstance.start();


        //or in your case
        new Thread(new VariableUpdater()).start();
        Thread.sleep(9000); //runs eventually after 9 seconds
        Accessor.print(); 

    }    
}

更新:

class Thread1 extends Thread{

    static List<String> list = new ArrayList<String>();

}

class OtherClass{

    public void someMethod(){
        Thread1.list; //this is how you access static variable of one class in other
    }
}

【讨论】:

  • 那不是我想要的。我需要在后台运行 VariableUpdater 类,并且需要从另一个偶尔运行的类访问列表。
  • 使用Thread.sleep(9000),它将最终模拟正在运行。检查更新的答案。
  • 这需要运行相同的类...这不是我想要的。
  • 您的问题不清楚。请用适当的例子详细说明。您不能启动两个主要方法并期望它们共享状态。
猜你喜欢
  • 2013-07-14
  • 2015-10-04
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
相关资源
最近更新 更多