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?

Community
  • 1
  • 1
gtiwari333
  • 24,554
  • 15
  • 75
  • 102

2 Answers2

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
    }
}
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
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();
  }
}
varsha
  • 33
  • 3
  • 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. – gtiwari333 Aug 31 '13 at 13:40