簡體   English   中英

如何執行任務並每分鍾返回數據

[英]How to perform a task and return a data every minute

我想創建一個執行某些任務並每分鍾返回一次列表的函數。 但是現在借助鏈接和其他一些鏈接,我可以打印結果但不返回結果。主要原因是run方法的返回類型為void,因此未更改為列表。

我的代碼如下:

 List<String> ignoreList=new ArrayList<>();
   Map<String,List<String>> deviceMap=new HashMap<>(); 
   List<String> newlyAddedUUIDList=new ArrayList<>(); 

    int MINUTES = 1; // The delay in minutes
    Timer timer = new Timer();
     timer.schedule(new TimerTask() {
        @Override
        public void run() { // Function runs every MINUTES minutes.
            // Run the code you want here
   String xSubjectToken=SecretIdRetreiver.getXSubjectToken();
   Map<String, List<String>> uuidInfo= SecretIdRetreiver.getUUIDList(xSubjectToken); // If the function you wanted was static
   System.out.println(uuidInfo);
   Set<String> uuidSetList=uuidInfo.keySet();
   for(String uuid: uuidSetList) {
       if(ignoreList.contains(uuid)) {
          System.out.println("UUid already in the ignore list"); 
       }else {
           if(!deviceMap.containsKey(uuid)) {
               Map<String,List<String>> resultOfSC1=SecretIdRetreiver.performStage1Config(xSubjectToken, uuid);
               System.out.println(resultOfSC1);
               if(resultOfSC1.containsKey("AddInIgnoreList")) {
                   List<String> result=resultOfSC1.get("AddInIgnoreList");
                   String uuidToBeIgnored=result.get(0);
                       ignoreList.add(uuidToBeIgnored);                    
               }else if(resultOfSC1.containsKey("Successful")) {
                   List<String> result=resultOfSC1.get("Successful");
                   System.out.println(result);
                   String deviceId=result.get(1);
//                 System.out.println(deviceId);
                   String[] deviceuUIdSplit=deviceId.split("\\.");
                   String duUID=deviceuUIdSplit[0];
                   System.out.println(duUID);
                   String serialNumber=result.get(0);
                   String secretNumber=result.get(2);
                   List<String> info=new ArrayList<>();
                   info.add(secretNumber);
                   info.add(serialNumber);
                   if(deviceMap.containsKey(duUID)) {

                   }else {
                       newlyAddedUUIDList.add(duUID);
                   }
                   deviceMap.put(duUID, info);

               }
           }else {
               System.out.println("Already Added in the Device list");
           }

       }

   }
   getNewAddedUUId(newlyAddedUUIDList);
//   System.out.println(newlyAddedUUIDList+"NewAdded UUIDs");
   newlyAddedUUIDList.clear();


        }


      }, 0, 1000 * 60 * MINUTES);

    public static List<String> getNewAddedUUId(List<String> newlyAdded) {
    // TODO Auto-generated method stub
    System.out.println(newlyAdded+"Newly Added UUIDs");
    return newlyAdded;

}

主要目的是將此代碼放置在一個函數中,該函數將每分鍾返回結果列表。

這就是問題所在-函數每次調用僅返回一次-而不是多次(例如,每分鍾一次)。

因此,每分鍾調用一次函數。

或將列表推到某個成員變量中,然后每分鍾告訴“呼叫者”去看看。

但是現在我們接近偵聽器模式了。 因此,在用戶界面中說我們有一個按鈕。 而且我們有多個部分的代碼都想在按下按鈕時得到通知-因此,我們將那些希望作為偵聽器得到通知的內容添加到按鈕中。 然后,當事件發生在按鈕上時,我們可以遍歷偵聽器列表,並告訴他們事件已發生。

此模式的稍微復雜一點的用法是Actions:

https://docs.oracle.com/javase/tutorial/uiswing/misc/action.html

https://docs.oracle.com/javase/7/docs/api/javax/swing/Action.html

這是生產者消費者方案。 您在時間線程中的任務是生產者,而您需要的是在另一個線程中消耗生產者結果的消費者。 這可以幫助您入門:

    BlockingQueue<Object> results = new LinkedBlockingDeque<>();

    //your task (aka producer)
    Runnable task = ()->{
        Object result = //compute your result
        results.add(result);
    };
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);        
    executor.scheduleAtFixedRate(task,0, 1, TimeUnit.MINUTES);

    //consumer
    while(true) {
        Object result = results.take();
    }

進行擴展TimerTask的自定義任務。 在其構造函數中添加一個適合您需求的列表。 然后在run方法中,只需修改您的列表即可。

public class MyTimerTask extends TimerTask {
    public MyTimerTask(List<MyObject> objects) {
        super();
        this.objects = objects;
    }
    private final List<MyObject> objects;

    @Override
     public void run() {
         // your logic
         objects.add(whatever);
     }

    public List<MyObject> getElements() {
         return objects;
    }
}

注意:簽出Java的並發類。 只有一個列表,但是有許多不同種類的隊列。 文檔是一個很好的起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM