1

I have a class like this , where I am updating a static variable in a thread. And I need to access this variable from another class.

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);
        }
    }
}

And this variable needs to be accessed from another class like this :

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

The problem is, when running the Accessor class the list is empty.

Am I missing something here?

UPDATE/Solution

It turns out we can achieve this by using Hazelcast or some sort of messaging/caching utility. I will post a full solution soon.

Source: How to share object between java applications?

3
  • You have two main methods. Assuming you start the last one: when do you start your thread? Commented Aug 31, 2013 at 13:13
  • I run the VariableUpdater class first. The logic updates the variable in each one second. And the list needs to be accessed from the Accessor class. Commented Aug 31, 2013 at 13:15
  • 1
    You have to two JVM instances. Check my answer. Commented Aug 31, 2013 at 13:33

2 Answers 2

1

From this code u can access the List in another class object

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();
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

that's not what i wanted. i need the VariableUpdater class running in background and the list needs to be accessed from another class which runs occasionally.
1

You have two main() methods in two different classes. On running two main() methods there will be two instances of JVM and those do not share anything. So your list will always be empty.

Use one main() method to start threads.

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(); 

    }    
}

UPDATE:

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
    }
}

4 Comments

that's not what i wanted. i need the VariableUpdater class running in background and the list needs to be accessed from another class which runs occasionally.
use Thread.sleep(9000) which will simulate the being run eventually. Check the updated answer.
this requires the same class to be ran... that's not what i wanted.
Your question is unclear. Please elaborate with a proper example. You can't start two main methods and expect them to share state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.