处理列表到一个线程
基本上我有一个整数数组,我想将其移交给线程,但我无法获得正确的语法。
// Create list
List <Integer> list = new ArrayList<Integer>();
// Create thread
TPServer server = new TPServer(port, <Integer> list);
new Thread(server).start();
// Below is the TPServer class
// TPServer Class
public class TPServer implements Runnable {
private List <Integer> list = null;
private int port = 0;
private boolean isStopped = false;
public TPServer(int port, List <Integer> list) {
this.list = list;
this.port = port;
}
}
Basically I have an integer array which i want to hand over to a thread, but i can't get the syntax right.
// Create list
List <Integer> list = new ArrayList<Integer>();
// Create thread
TPServer server = new TPServer(port, <Integer> list);
new Thread(server).start();
// Below is the TPServer class
// TPServer Class
public class TPServer implements Runnable {
private List <Integer> list = null;
private int port = 0;
private boolean isStopped = false;
public TPServer(int port, List <Integer> list) {
this.list = list;
this.port = port;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还没有实现
Runnable
,它也应该是new TPServer(port, list);
You haven't implemented
Runnable
and also it should benew TPServer(port, list);
您可以像传递任何其他参数一样传递通用参数。
You pass a generic argument the same as any other.
您可能想要使用使用信号量的列表来防止并发访问,或者使用线程安全的列表类型(如果您的列表也在线程外使用)。 (如果它仅在线程内部使用,显然在内部创建它:=))
You might wanna use a list that uses a semaphore to prevent concurent access Or use a list type that is thread safe, if your list is being used also out of the thread. (and if it is used only inside the thread, create it inside obviously :=) )