【发布时间】:2016-01-22 22:19:15
【问题描述】:
我需要一点关于 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();
}
}
线程 Descoberta 将根据需要将新项目添加到 gatewaysCadastrados 列表中。并且发布线程将读取此列表并为列表中的每个对象执行一个操作。
我只需要知道如何共享这个变量并将其传递给线程。我需要构建一个信号量来执行此操作吗?
【问题讨论】:
-
这里有一些关于集合的好信息:java-performance.info/java-collections-overview - 包括对可用多线程集合的描述。
-
将对象传递给可运行对象。这是一个类似的问题:stackoverflow.com/questions/17756255/…
标签: java multithreading