为什么我的可运行接口循环我的队列 继续这里



在这里,我想以有限的Queue b大小运行我的所有列表a如果一个元素b.poll()(从队列中删除并使用它)它再次从List a中添加一个元素,依此类推......

我已经尝试了多种方法来停止循环。

public class BroadcastThreadInitializerProcessor implements Runnable {
@Autowired
private NetworkInterfaceServiceInterface networkInterfaceServiceInterface;
private volatile boolean running = true;
private volatile boolean paused = false;
private VoiceBroadcast voiceBroadcast =null;
List<ExcelDatas> a = null;
public BroadcastThreadInitializerProcessor(VoiceBroadcast voiceBroadcast, UploadExcel uploadExcel ) {
this.voiceBroadcast=voiceBroadcast;
this.a = uploadExcel.getExcelDatas();
}
@Override
public void run() {
while (running){
synchronized (a) {
if (!running) { 
break;
}
if (paused) {
try {
a.wait(); 
} catch (InterruptedException ex) {
break;
}
if (!running) { // running might have changed since we paused
break;
}
}
}//syncronized stop
// Broadcasting code START here...
int channels = Integer.parseInt(voiceBroadcast.getChannel());
try {
Queue<ExcelDatas> b = null;
int aIndex  = 0;
if(a.size()> channels){  //load elements in Queue
b = new LinkedList<>();
for (int i = 0; i < channels ; i++) {
b.add(a.get(aIndex));
aIndex++;
}
}else {
b = new LinkedList<>(a); //load all elements
}
Client client = new Client();
client.connect("",  , "", 10); 
client.setEventSubscriptions( "", "" );
boolean looping = true;
while (looping) {   //looping while
if (paused) {
a.wait();
}
if (!running) { looping=false; break;} // break when all call are completed
if (b.isEmpty()) { running=false; break;} // break when all call are completed
EslMessage chan = client.sendSyncApiCommand("show channels count","");
int usedChannel = Integer.parseInt(Arrays.asList(chan.getBodyLines().toString().replaceAll("[^0-9]+"," ").trim().split(" ")).get(0));
if(usedChannel<(channels-1)) {
System.err.println("Used Channels "+usedChannel +" <--Total Channels"+channels);
ExcelDatas frontValue = b.poll(); // get queue's first element and remove it
String uuid = UUID.randomUUID().toString();
if(frontValue.getContact().length()>=10) {
client.sendSyncApiCommand("",""); //for calling
System.err.println("Callling on "+frontValue.getContact()+" By clip number:-"+voiceBroadcast.getClip()+" with UUID:-"+uuid);
}//if for Contact number
//call Processs    Stop  
if(a.size()> channels){
if (aIndex  < a.size()) {
b.add(a.get(aIndex++)); //Add of New Node in Queue
}//inner if node set
}//if upper
//for close all loops if b is empty
if(b.isEmpty()) {
System.err.println("b is empty");
running = false; 
looping=false;
break;
}//if
Thread.sleep(700);
}//upper if   ports check
}//Stop looping while loop
looping=false;
running = false; 
client.close();
}catch (Exception e) {
}          
// Broadcasting code STOP here...           
}//while loop
}//run() method STOP

// Actions methods Start
public void terminate() {
running = false;
}
public void pause() {
paused = true;
}
public void resume() {
synchronized (a) {
paused = false;
a.notifyAll(); // Unblocks thread
}
}
//Action Methods STOP   
}

但是我的代码做出了反应,因为第一个队列大小一次又一次地重复运行,只有这些元素(if a.size()>channel where channel is b.size()),它不会NOR Exit the while(looping)下一个元素。

任何人都可以solve/explain/describe吗?谢谢。。。

在这里我只是改变了,

private static  BlockingQueue<ExcelDatas> queue = null;
......
......
queue = new ArrayBlockingQueue<>(channels);
.......
.......
ExcelDatas frontValue = queue.take();

它解决了我的问题,谢谢@AntoineMarques。

最新更新