简体   繁体   English

多线程共享变量

[英]Multi-threading shared variables

I have a multi-threading application developed in Java. 我有一个用Java开发的多线程应用程序。 The new threads fuction is to run network protocols clients and servers. 新的线程功能是运行网络协议客户端和服务器。

My problem is in the UDP "thread" to find other instances in the local network and store them in a List. 我的问题是在UDP“线程”中找到本地网络中的其他实例并将它们存储在列表中。 But all the new threads are named threads called by other classes (main thread). 但是所有新线程都被其他类(主线程)调用了命名线程。 So my problem is to get the processed list after the thread is finished. 所以我的问题是线程完成后要获取处理后的列表。

I have tried to pass the list to constructor parameter and I make the main thread wait for the end of that thread but then the list is empty when it should be with some elements. 我试图将列表传递给构造函数参数,并让主线程等待该线程的结束,但是当列表应包含某些元素时,列表为空。

Thanks. 谢谢。

EDIT with an example code 使用示例代码进行编辑

public class MainClass{

public static void main(String[] args) {

    List<String> tmp = new ArrayList<>();
    Thread t1 = new Thread(UDPClient(tmp));
    t1.start();
    t1.join();
    //It should be one but it'll be 0
    System.out.println(tmp.size);

}

}

and the separeted class: 和分隔的班级:

public class UDPClient implements Runnable{

private List<String> foundInstances;

public UDPClient(List<String> instances)
{
    foundInstances = instances;
}

public void run()
{
    //do stuff
    foundInstances.add("Hello world");
}
}

it's a simple example... 这是一个简单的例子

You can use any of various approaches to communicate between threads. 您可以使用各种方法中的任何一种在线程之间进行通信。 The following sample code illustrates a straightforward one. 下面的示例代码说明了一个简单的代码。

import java.util.ArrayList;
import java.util.List;

public class MultiThreaded {

    private static class ListAppender implements Runnable {

        private List<String> strings;
        private String string;

        public ListAppender(List<String> strings, String string) {
            this.strings = strings;
            this.string = string;
        }

        @Override
        public void run() {
            strings.add(string);
        }
    }

    private static class ListConsumer implements Runnable {

        private List<String> strings;

        public ListConsumer(List<String> strings) {
            this.strings = strings;
        }

        @Override
        public void run() {
            for (String s : strings) {
                System.out.println(s);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException{
        List<String> strings = new ArrayList<String>();

        Thread helloThread = new Thread(new ListAppender(strings, "Hello"));
        helloThread.start();
        Thread worldThread = new Thread(new ListAppender(strings, "world!"));
        worldThread.start();

        Thread.sleep(1000);

        new Thread(new ListConsumer(strings)).start();
    }
}

All three threads write to and read from the same list instance. 所有三个线程都写入和读取相同的列表实例。 Note that the above approach is not actually thread safe by any measure. 请注意,上述方法实际上无论如何都不是线程安全的。 You should instead use a thread safe collection class. 您应该改为使用线程安全集合类。

The Thread.sleep() allows the ListAppender instances to complete before the ListConsumer is run. Thread.sleep()允许ListAppender实例在运行ListConsumer之前完成。 Using Thread.sleep() like this is hardly a robust approach, and should probably be avoided in any real world scenario. 像这样使用Thread.sleep()几乎不是一种健壮的方法,在任何现实情况下都应避免使用。

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

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