简体   繁体   English

从线程获取要由函数使用的变量

[英]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, 我承认我对Java的理解不是很好,这是我第一次使用Threads ,所以就这样,

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: 编辑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. 编辑以显示完整代码,基本上我想要的是将要从线程传递给onMessage函数的用户列表。 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? 我尝试了一些getter和setter方法,但是由于我认为必须使用同步化功能才能正常工作,所以该方法不起作用?

Edit 2: 编辑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. 您可能需要扩展Thread类,并创建一个构造函数,该构造函数采用您事先实例化的对象并将该对象传递给构造函数。 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. 您也可以在MockDataGenerator内部和new Thread语句外部创建对象。 This will also be accessible outside the thread with the changes. 更改之后,也可以在线程外部访问此文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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