Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有共享列表的Java多线程_Java_Multithreading - Fatal编程技术网

具有共享列表的Java多线程

具有共享列表的Java多线程,java,multithreading,Java,Multithreading,我需要一些关于java多线程的帮助。我有这门课: public class EdgeServer{ private static final int ServidorBordaID = 9; private static final String urlLogin = "http://localhost/exehdager-teste/index.php/ci_login/logar"; private static final String insertSensorU

我需要一些关于java多线程的帮助。我有这门课:

public class EdgeServer{

    private static final int ServidorBordaID = 9;
    private static final String urlLogin = "http://localhost/exehdager-teste/index.php/ci_login/logar";
    private static final String insertSensorURI = "http://localhost/exehdager-teste/index.php/cadastros/ci_sensor/gravaSensor";
    private static final String insertGatewayURI = "http://localhost/exehdager-teste/index.php/cadastros/ci_gateway/gravaGateway";
    private static ArrayList<Gateway> gatewaysCadastrados = new ArrayList<>();

    public static void main(String[] args) {
        // Start a user thread that runs the UPnP stack
        Thread clientThread = new Thread(new Descoberta());
        clientThread.setDaemon(false);
        clientThread.start();

        Thread publicationThread = new Thread(new Publication());
        publicationThread.setDaemon(false);
        publicationThread.start();
    }
}
公共类边缘服务器{
专用静态最终int ServidorBordaID=9;
私有静态最终字符串urlLogin=”http://localhost/exehdager-teste/index.php/ci_login/logar";
私有静态最终字符串insertSensorURI=”http://localhost/exehdager-teste/index.php/cadastros/ci_sensor/gravaSensor";
私有静态最终字符串insertGatewayURI=”http://localhost/exehdager-teste/index.php/cadastros/ci_gateway/gravaGateway";
私有静态ArrayList网关scadastrados=新ArrayList();
公共静态void main(字符串[]args){
//启动运行UPnP堆栈的用户线程
线程clientThread=新线程(new Descoberta());
setDaemon(false);
clientThread.start();
Thread publicationThread=新线程(new Publication());
publicationThread.setDaemon(false);
publicationThread.start();
}
}
线程Descoberta将根据需要向网关SCADASTRADOS列表添加新的ITEN。发布线程将读取此列表,并对列表中的每个对象执行一个操作


我只需要知道如何共享这个变量并将其传递给线程。我需要构建一个信号量来实现这一点吗?

以下是示例代码,您可以在两个线程之间共享列表,并且需要使用wait和notify作为信号量

public class Descoberta extends Thread {
private  final ArrayList<Gateway> a = new ArrayList<>();
public Descoberta( ArrayList<Gateway> a) {
        this.a = a;
    }

    @Override
    public void run() {
         synchronized (a) {
                while(true){ // your condition
                    a.wait();
                }
                a.notify();
         }
    }
}

public class Publication extends Thread {
private  final ArrayList<Gateway> b = new ArrayList<>();
public Publication(ArrayList<Gateway> b) {
        this.b = b;
    }

    @Override
    public void run() {
         synchronized (b) {
                while(true){ // your condition
                    b.wait();
                }
                b.notify();
         }
    }
}

public class EdgeServer {
    public static void main(String args[]) {
        private final ArrayList<Gateway> gatewaysCadastrados = new ArrayList<>();
        Thread clientThread = new Descoberta(gatewaysCadastrados);
        Thread publicationThread = new Publication(gatewaysCadastrados);
        clientThread.start();
        publicationThread.start();
    }
}
公共类Descoberta扩展线程{
私有最终ArrayList a=新ArrayList();
公共Descoberta(ArrayList a){
这个a=a;
}
@凌驾
公开募捐{
已同步(a){
而(对){//你的情况
a、 等待();
}
a、 通知();
}
}
}
公共类发布扩展了线程{
私有最终ArrayList b=新ArrayList();
公共出版物(ArrayList b){
这个.b=b;
}
@凌驾
公开募捐{
已同步(b){
而(对){//你的情况
b、 等待();
}
b、 通知();
}
}
}
公共类边缘服务器{
公共静态void main(字符串参数[]){
私有最终ArrayList网关scadastrados=新ArrayList();
线程clientThread=new Descoberta(网关scadastrados);
Thread publicationThread=新出版物(gatewaysCadastrados);
clientThread.start();
publicationThread.start();
}
}

将共享对象作为构造函数参数传递给相关Runnable的简单方法;e、 g

Thread clientThread = new Thread(new Descoberta(gateways));
...
Thread publicationThread = new Thread(new Publication(gateways));
显然,相应的
Runnable
构造函数需要保存参数,以便它们的
run()
方法可以找到它们

还有多种其他方式:

  • 如果可运行项是同一外部类实例中的内部类,则它们可以访问外部类中的共享对象

  • 如果共享状态存储在
    static
    变量中,那么可运行程序可以通过静态getter等访问它们。(注意:这很可能是糟糕的设计…)



正如@Fidor指出的,如果两个或多个线程要共享一个公共(可变)数据结构,那么它们需要同步数据结构上的读写操作。如果忽略这一点,应用程序可能会出现难以复制和追踪的潜在错误。

此处有一些关于集合的好信息:-包括可用多线程集合的描述。将对象传递给可运行程序。这里有一个类似的问题:@ Hubertokf,如果你以这种方式共享资源,你需要考虑该资源的同步。“并发”命名空间已经有一些数据结构在为您执行此操作。但是这些线程将共享相同的数据吗?我的意思是,如果ThreadClientThread在gatewaysCadastrados上插入一个项目,publicationThread会看到它吗?假设只有一个线程会修改列表,我真的需要一个信号量吗?@Hubertokf看看例如“CopyOnWriteList”。对不起,@Fildor耶!我用这个做了一份名单。。非常感谢!