-1

My project has a demand, need to constantly read the bar code data, like a commodity supermarket scanner with bar code scanning guns, then data into a keypad, but encountered a problem, for a long time continuously scanning, CPU usage will be very high, even reached 95%, I have set the thread to sleep in a loop, but failed to solve this problem.

I have been asking for this problem, but it may be too messy code, affecting everyone to read, and now simplify the code, I hope you can help me, thank you very much;

Sometimes a few hours on the CPU scan occupy too high, but sometimes a few days there. Grab logcat log found the sleep method sometimes is not executed, if not continuous execution will cause CPU use rate is too high, but I don't know why the sleep method will not perform .

private void startReceive() {
    stopReceive = false;
    new Thread(new Runnable() {
        @Override
        public void run() {
            int timeout = 1000;
            while (!stopReceive) {
                if (mUsbDeviceConnection != null) {
                    try {
                        byte[] receiveBytes = new byte[64];
                        int value = mUsbDeviceConnection.bulkTransfer(mUsbEndpoint, receiveBytes,
                                receiveBytes.length, timeout);
                        if (value > 0) {
                            for (int i = 2; !stopReceive && i < receiveBytes.length; i++) {
                                byte b = receiveBytes[i];
                                if (b != 0) {
                                    result += new String(new byte[]{b});
                                }
                                if (!stopReceive && !result.equals("") && result != null) {
                                    Runtime.getRuntime().exec("input text " + result);
                                }
                            }
                        }
                        Thread.sleep(100);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }).start();
}
Victor
  • 11
  • 7
  • Possible duplicate of [Application CPU Usage is high in android?](http://stackoverflow.com/questions/14688222/application-cpu-usage-is-high-in-android) – mike vorisis Jan 03 '17 at 10:03

2 Answers2

0

You should better look this post and try to find which method consume more system resource: https://stackoverflow.com/a/14688291/6176003

Community
  • 1
  • 1
Paraskevas Ntsounos
  • 1,755
  • 2
  • 18
  • 34
  • I know which method will consume more resources, but it is impossible to change things, sometimes a few hours on the CPU scan occupy too high, but sometimes a few days there. Grab logcat log found the sleep method sometimes is not executed, if not continuous execution will cause CPU use rate is too high, but I don't know why the sleep method will not perform – Victor Jan 05 '17 at 12:41
0

This seemd to be a huge thread running on the main-thread which will drastically slow down the performance of the device.

Big operations you should instead run asynchronously, which means that it will run in the background-thread and not affect the UI-thread which is the issue right now:

Here's a example of how the implementation would look like:

    private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
      // StartReceive code..
      stopReceive = false;
      new Thread(new Runnable() {
          @Override
          public void run() {
              int timeout = 1000;
              while (!stopReceive) {
                  if (mUsbDeviceConnection != null) {
                      try {
                          byte[] receiveBytes = new byte[64];
                          int value = mUsbDeviceConnection.bulkTransfer(mUsbEndpoint, receiveBytes,
                                  receiveBytes.length, timeout);
                          if (value > 0) {
                              for (int i = 2; !stopReceive && i < receiveBytes.length; i++) {
                                  byte b = receiveBytes[i];
                                  if (b != 0) {
                                      result += new String(new byte[]{b});
                                  }
                                  if (!stopReceive && !result.equals("") && result != null) {
                                      Runtime.getRuntime().exec("input text " + result);
                                  }
                              }
                          }
                          Thread.sleep(100);
                      } catch (Exception ex) {
                          ex.printStackTrace();
                      }
                  }
              }
          }
      }).start();
      return "Done";
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // We're done
    }

    @Override
    protected void onPreExecute() {
      // Before starting operation
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

How to start the thread:

LongOperation longOp = new LongOperation();
longOp.execute();

Read more: AsyncTask Android example

Community
  • 1
  • 1
timbillstrom
  • 1,176
  • 16
  • 33
  • I have tried to use the AsyncTask, it doesn't work ; – Victor Jan 05 '17 at 12:08
  • @Victor What do you mean by 'it doesn't work'? Is the CPU usage still too high or is there any problems with the code itself? – timbillstrom Jan 05 '17 at 12:10
  • The CPU usage is still too high, CPU occupancy rate is still high, I take a look at the AsyncTask source, internal maintenance of a BlockingQueue, so the thread management does help, but in addition to the main thread, I only use a sub thread; using AsyncTask scan 4 to 5 hours, the CPU rate will rise to a high – Victor Jan 05 '17 at 12:56
  • @Victor is it possible that there might be something else in the code which is causing this huge load? – timbillstrom Jan 06 '17 at 15:54
  • First of all thank you for the solution, I have been using AsyncTask instead of Thread, because it is more secure;But now have a new problem, I use the Runtime.getRuntime.exec("system/bin/input text ");Process.waitFor();The Process.waitFor() can guarantee the order procedure, but when I remove the barcode, don't have at the time of scanning, the while loop continued to perform, after a period of time to stop, I use the Process.destroy(), but no effect;Do you have any good suggestions, thank you very much. "-" "-" – Victor Jan 11 '17 at 10:22