简体   繁体   中英

Getting a variable from a thread to be used by a function

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 Threads , so here it goes,

public MockDataGenerator() {

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

                        System.out.println("These are the bayeuxIds from new Thread" + bayeuxIds);
                        List<String> users = new ArrayList<String>();
                        users.addAll(bayeuxIds);
                        setData(users);
                        Collections.shuffle(users);
                        String bayeuxId = users.get(0);
                        List<Alert> 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");
    }

}

Edit 1:

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?

Edit 2:

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

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

You may want to extend the Thread class and create a constructor that takes an object that you instantiate before hand and pass the object in to the constructor. This will allow the Thread to alter the object and be able to see the changes outside the thread. This is the cleanest way.

Also you can create an object inside the MockDataGenerator and outside the new Thread statement. This will also be accessible outside the thread with the changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM