簡體   English   中英

樹莓派與Java應用程序的CPU使用率高

[英]Raspberry pi with java application high CPU usage

我在樹莓派上運行了一個Java應用程序,但大多數時候它崩潰了。 每當它崩潰時,通常Java都會有很高的CPU使用率(> 100%)。 我的應用程序如何工作:我有一個RFID閱讀器,可以讀取標簽,並且每當讀取標簽時,都會調用messageReceived方法。 該方法將讀取的標簽存儲在特定集中。 然后,我創建一個新線程來偵聽套接字,並且在套接字打開且設置更改后,該線程調用一些javafx方法來打開新屏幕。 但是,當我將應用程序部署到樹莓派時,它會隨機崩潰,並且Java的CPU使用率很高。 如果我忘了解釋什么,請隨時提出任何問題。

編輯1:我的線程類。 編輯2:現在我的問題是:為什么我有這么高的CPU使用率,以及如何解決它。

public class RFIDThread implements Runnable {
    /**
     * The socket for the connection to the LLRP Reader
     */
    private Socket socket;

    private JSONArray valid_tags;

    private JSONArray found_tags;

    private TagsListController controller;

    private RFIDSet rfidset;

    /**
     * Thread for constant reading of the stream
     * 
      * @param socket
      * @param controller
      * @param tags
     */
    public RFIDThread(Socket socket, TagsListController controller, JSONArray tags, RFIDSet rfidset) {
         this.socket = socket;
         this.controller = controller;
         this.rfidset = rfidset;
         this.found_tags = new JSONArray();
         this.valid_tags = tags;
    }


     /**
      * Runnable for this thread.
      * First get all the found tags from the xml controller
      * Then loop over the rfid set to find any new tags.
      * If there are any, display them.
      */
    @Override
    public void run() {
        CopyOnWriteArrayList<Tag> originalSet = new CopyOnWriteArrayList<>();
        originalSet.addAll(rfidset.getSet());
        boolean started = true;

        if (socket.isConnected()) {
            while (!socket.isClosed()) {
                CopyOnWriteArrayList<Tag> set = new CopyOnWriteArrayList<>();
                set.addAll(rfidset.getSet());
                if(started || !originalSet.equals(set)) {
                    started = false;
                    CopyOnWriteArrayList<String> found_set = new CopyOnWriteArrayList<>();
                    found_set.addAll(controller.found_tags_list.getItems());                                      

                    this.found_tags.clear();
                    this.found_tags.addAll(found_set);


                    for (Tag found_tag : set) {
                        if (found_tags.indexOf(found_tag.getId()) < 0) {
                            Integer index = valid_tags.indexOf(found_tag.getId());
                            if (index >= 0) {
                                Platform.runLater(() -> controller.showValid(found_tag.getId()));
                            } else {
                                Platform.runLater(() -> controller.showError(found_tag.getId()));
                            }

                            found_tags.add(found_tag.getId());

                            pause(5000);
                        }
                    }

                    originalSet = set;

                    pause(5000);
                }
            }
        }
    }


    /**
     * Close the socket
     */
    public void shutdown() {
       try {
           this.socket.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
    }

    private void pause(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

嘗試移動pause(5000); if (started || !originalSet.equals(set)) {語句之外。

高CPU使用率通常是一個緊密的循環,沒有暫停,I / O或等待的東西。 就您而言,每當originalSet.equals(set)都不會暫停。

您可能更喜歡使用:

                if (started || !originalSet.equals(set)) {
                    // ...
                } else {
                    pause(0);
                }

或類似。

暫無
暫無

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

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