0
멀티 스레드를 구현 중이며 메인에서 각 스레드와 메시지를주고받을 수 있기를 원합니다. 그래서 나는 다음과 같은 코드를 각 스레드에 대한 설정에 차단 큐를 시도하고있다 :블로킹 큐 배열 만들기
public static void main(String args[]) throws Exception {
int deviceCount = 5;
devices = new DeviceThread[deviceCount];
BlockingQueue<String>[] queue = new LinkedBlockingQueue[5];
for (int j = 0; j<deviceCount; j++){
device = dlist.getDevice(); //get device from a device list
devices[j] = new DeviceThread(queue[j], device.deviceIP, port, device.deviceID, device.password);
queue[j].put("quit");
}
}
public class DeviceThread implements Runnable {
Thread t;
String ipAddr;
int port;
int deviceID;
String device;
String password;
BlockingQueue<String> queue;
DeviceThread(BlockingQueue<String> q, String ipAddr, int port, int deviceID, String password) {
this.queue=q;
this.ipAddr = ipAddr;
this.port = port;
this.deviceID = deviceID;
this.password = password;
device = "device"+this.deviceID;
t = new Thread(this, device);
System.out.println("device created: "+ t);
t.start(); // Start the thread
}
public void run() {
while(true){
System.out.println(device + " outputs: ");
try{
Thread.sleep(50);
String input =null;
input = queue.take();
System.out.println(device +"queue : "+ input);
}catch (InterruptedException a) {
}
}
}
}
코드는 컴파일하지만 런타임 동안 그것은 단지 한 큐와 함께 일 queue[j].put("quit");
라인에 나에게 NullPointerException를 돌려 "예상한다"
사람이이 문제를 해결하는 방법을 알고 배열이 제대로 초기화 밤은 때문에, 내가 BlockingQueue[] queue = new LinkedBlockingQueue10;
로 선언 시도는 내가 생각하지만 그것이 나에게주는 BlockingQueue queue = new LinkedBlockingQueue(5);
? netbeans IDE 7.3.1을 사용하고 있습니다.
감사합니다.
가능한 복제 : http://stackoverflow.com/questions/2564298/java-how-to-initialize-string – Gray