首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的Runnable接口循环我的队列在这里继续

为什么我的Runnable接口循环我的队列在这里继续
EN

Stack Overflow用户
提问于 2019-05-22 15:41:21
回答 1查看 52关注 0票数 1

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

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

代码语言:javascript
复制
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   
}

但是我的代码react as First Queue size一次又一次地重复运行,只有这些元素(if a.size()>channel where channel is b.size()),而不是下一个元素NOR Exit the while(looping)

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-01 13:26:37

在这里我只是改变了,

代码语言:javascript
复制
private static  BlockingQueue<ExcelDatas> queue = null;
......
......
 queue = new ArrayBlockingQueue<>(channels);
.......
.......
ExcelDatas frontValue = queue.take();

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56251672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档