Я пытаюсь писать и читать из ArrayBlockingQueue через потоки. У меня есть фабрика потоков и рабочие потоки, но по какой-то причине я не могу прочитать данные, которые я передаю рабочим потокам, в рабочих потоках. Любая идея? Спасибо.
public class CommunicationThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable runnable){
Thread thread=new Thread(runnable);
return thread;
}
}
public class ThreadEx implements Runnable {
private byte[] pack;
private final BlockingQueue writeBlockingQueue;
public Writer(BlockingQueue writeBlockingQueue, byte[] pack) {
this.writeBlockingQueue = writeBlockingQueue;
this.pack = pack;
}
@Override
public void run() {
TimeUnit.SECONDS.sleep(1);//Even this line block it wont process below this line. Even after time out.
writeBlockingQueue.put(pack);//Tried even with disabling put and takes from queue. still did not work
System.out.println("In Thread "+Thread.currentThread().getName() +" got "+ this.pack.length);// This line does not even produce output.
writeBlockingQueue.take();
}
}
public static void main(String[] args) throws UnsupportedEncodingException {
ExecutorService connectionThreadPool = Executors.newFixedThreadPool(15,new
CommunicationThreadFactory());
BlockingQueue<byte[]> blockingQueue = new ArrayBlockingQueue<>(10, true);
byte[] packet = new byte[]{0x63, 0x41, 0x35, 0x19};
for (int i = 0; i < 10; i++) {
connectionThreadPool.execute(new ThreadEx());
}
}