Getting a variable from a thread to be used by a function
<p>This may be a really simple question, but things I've tried don't seem to work. I admit my understanding of Java is not that great and this is the first time I've worked with <code>Threads</code>, so here it goes,</p>

<pre><code>public MockDataGenerator() {

    new Thread() {
        @Override
        public void run() {
            while (true) {
                try {
                    if (bayeuxIds.size() &gt; 0) {

                        System.out.println("These are the bayeuxIds from new Thread" + bayeuxIds);
                        List&lt;String&gt; users = new ArrayList&lt;String&gt;();
                        users.addAll(bayeuxIds);
                        setData(users);
                        Collections.shuffle(users);
                        String bayeuxId = users.get(0);
                        List&lt;Alert&gt; alerts = generateRandomAlerts(1);
                        alertsAdded(bayeuxId, alerts);

                    }
                } catch (Throwable t) {

                } finally {
                    try {
                        int numUsers = bayeuxIds.size();

                        // plus 1 prevents divide by zero
                        sleep(30000);
                        //sleep(r.nextInt(120000 / (numUsers + 1)));

                    } catch (InterruptedException e) {
                    }
                }
            }
        }
    }.start();

}

public void onMessage(Message message) {
    System.out.println("Message recieved from ActiveMQ");


    if (message instanceof TextMessage) {
        try {

            System.out.println("This is the 0th element of usersCopy: " + users.get(0));
            String bayeuxId = users.get(0);


            String theMsg = ((TextMessage) message).getText();
            clientMessanger.sendUpdate(theMsg, bayeuxId);

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        throw new IllegalArgumentException("Message must be of type TextMessage");
    }

}
</code></pre>

<p><strong>Edit 1:</strong></p>

<p>Edited to show the full code, basically what I want is the users List from the Thread to be passed into the onMessage function. I tried some getters and setters but that did not work since I assume I have to use a syncronized function in order for that to work right? </p>

<p><strong>Edit 2:</strong></p>

<p>Added these functions but it still doesn't seem to be setting the variables right. What am I doing wrong?</p>

<pre><code>    String bayeuxIdCopy;

public synchronized void setData(String bayeuxId) {
    System.out.println("This is the bayeuxID being set:  " + bayeuxId);
    bayeuxIdCopy = bayeuxId;
}

public synchronized String getData() {
    System.out.println("This is the bayeuxID being returned:  " + bayeuxIdCopy);
    return bayeuxIdCopy;
}
</code></pre>
