小编Jro*_*Jro的帖子

Java 如何避免在循环中使用 Thread.sleep()

从我的主要开始,我开始了两个名为生产者和消费者的线程。两者都包含while(true)循环。生产者循环是 UDP 服务器,因此它不需要睡眠。我的问题出在消费者循环中。消费者循环从链接队​​列中删除对象并将其传递给函数以进行进一步处理。根据研究,在循环中使用线程睡眠不是一个好习惯,因为有时 O/S 不会在设置时间结束时释放。如果我在应用程序理想时删除线程睡眠,它会将 CPU 拖到 20% 到 30%。

class Producer implements Runnable {
    private DatagramSocket dsocket;
    FError fer = new FError();

    int port =1548;
    ConcurrentLinkedQueue<String> queue;

    Producer(ConcurrentLinkedQueue<String> queue){
        this.queue = queue; 
    }

    @Override
    public void run() {

        try {

            // Create a socket to listen on the port.
            dsocket = new DatagramSocket(port);
            // Create a buffer to read datagrams into.
            byte[] buffer = new byte[30000];
            // Create a packet to receive data into the buffer
            DatagramPacket …
Run Code Online (Sandbox Code Playgroud)

java producer-consumer

11
推荐指数
2
解决办法
1万
查看次数

安卓BLE扫描

O/S Android 11。我开发了一个带有 BLE 扫描的应用程序。它不与 BLE 设备进行任何连接。它只是获取附近设备的列表。应用程序运行良好。每次停止和启动扫描时,我都可以在 Logcat 中看到以下错误。谁能告诉我出了什么问题吗?

E/BtGatt.GattService: [GSIM LOG]: gsimLogHandler, msg: MESSAGE_SCAN_START, appName: com.ttt.ttt.vctap, scannerId: 11, reportDelayMillis=0
E/BtGatt.GattService: [GSIM LOG]: gsimLogHandler, msg: MESSAGE_SCAN_STOP, appName:com.ttt.ttt.vctap, scannerId: 11, reportDelayMillis=0 
Run Code Online (Sandbox Code Playgroud)

java android bluetooth-lowenergy

1
推荐指数
1
解决办法
1829
查看次数