java 线程里的 while(true){sleep},[java example]你还在使用while(true) Thread.sleep吗?

7f8e9c2da9743b186f2093a6ec6a04f0.png

做各种SERVICE的时候,常常会需要一个程序重复定时地执行,基本上常见的山寨写法都是如下所示:

while (true) {

System.out.println("yours code");

Thread.sleep(3000);

}

但实际这样的写法可控性很低,JDK的java.util.concurrent中提供了大量的方法去控制一段代码定时执行,标准的改写上面的代码如下:

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

scheduler.scheduleWithFixedDelay(new Runner(), 0, 3, TimeUnit.SECONDS);

public class Runner implements Runnable {

public void run() {

System.out.println("yours code");

}

}

scheduler下有很多的方法,达到的目的有:定时执行,执行完后一段时间再次执行;而Executors更有更多控制线程的方法,平时不可忽视。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that! Here is a sample code in Java for reading strings from a `ConcurrentLinkedQueue` using multiple threads: ``` import java.util.concurrent.ConcurrentLinkedQueue; public class ConcurrentQueueReader { // Shared queue to read from private ConcurrentLinkedQueue<String> queue; public ConcurrentQueueReader(ConcurrentLinkedQueue<String> queue) { this.queue = queue; } // Method to be executed by each thread private void readFromQueue() { while (true) { // Check if the queue is empty if (queue.isEmpty()) { try { // Wait for a short period before trying again Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } else { // Retrieve the next string from the queue and process it String str = queue.poll(); System.out.println(str); } } } // Start multiple threads to read from the queue public void startReading(int numThreads) { for (int i = 0; i < numThreads; i++) { Thread t = new Thread(() -> readFromQueue()); t.start(); } } } ``` To use this code, you can create an instance of `ConcurrentQueueReader` and pass your `ConcurrentLinkedQueue` as a parameter. Then, call the `startReading()` method to start multiple threads, each of which will read strings from the queue and process them. For example: ``` public static void main(String[] args) { // Create a shared queue ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>(); // Add some strings to the queue queue.add("String 1"); queue.add("String 2"); queue.add("String 3"); // Create an instance of ConcurrentQueueReader and start reading from the queue ConcurrentQueueReader reader = new ConcurrentQueueReader(queue); reader.startReading(2); // Start 2 threads to read from the queue } ``` This code will output the following: ``` String 1 String 2 String 3 ``` And each string will be processed by one of the two threads that are reading from the queue. Note that the `ConcurrentQueueReader` class uses a `while` loop to continuously check the queue for new strings, and waits for a short period if the queue is empty. This ensures that each thread stays active and continues to read from the queue as new strings are added.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值