简体   繁体   English

Java:如何每秒创建一个新线程

[英]Java: How to make a new thread every second

I have a thread in Java that makes a web call and stores the information retrieved, but it only retrieves information for that particular instant. 我在Java中有一个线程可以进行网络调用并存储检索到的信息,但是它仅检索该特定瞬间的信息。 I'd like to run this thread every second for a certain period of time to get a better view of the data. 我想在一段时间内每秒运行一次此线程,以更好地查看数据。 How can I do this? 我怎样才能做到这一点? I've looked at ScheduledExecutorService, and from what I can tell if the thread is still running when it's time to set up the next run, it waits until the first thread is complete, which isn't what I'm looking for. 我已经查看了ScheduledExecutorService,从我可以知道何时设置下一次运行时线程是否仍在运行,它一直等到第一个线程完成时,这不是我想要的。

What you need is the scheduleAtFixedRate method: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit) 您需要的是scheduleAtFixedRate方法: http : //docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long, %20java.util.concurrent.TimeUnit)

When the scheduler waits until the first thread is complete, it's because you're using scheduleWithFixedDelay. 当调度程序等到第一个线程完成时,这是因为您正在使用scheduleWithFixedDelay。

However, if you absolutely want the threads run concurrently, you should try this: 但是,如果您绝对希望线程同时运行,则应尝试以下操作:

    pool.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            pool.submit(myJob);
        }
    }, 1, 1, TimeUnit.SECONDS);

I advise to always use a pool. 我建议始终使用游泳池。

You can do this by a double schedule. 您可以按照双重时间表进行操作。 Use scheduleWithFixedDelay() to set off a job every second. 使用scheduleWithFixedDelay()每秒设置一次作业。 This job starts the method which you really want to run. 这项工作将启动您真正要运行的方法。 Here is some code based on Oracle's ScheduledExecutorService API. 这是一些基于Oracle ScheduledExecutorService API的代码。

The Thread.sleep() is there to simulate a long-running task. Thread.sleep()可以模拟长时间运行的任务。

class Beeper {
   public static void main(String[] args) {
      (new Beeper()).beep();
   }
   private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

   public void beep() {
       final Runnable beeper = new Runnable() {
           public void run() { 
               System.out.println("beep"); 
               try {
                   Thread.sleep(10000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       };
       final Runnable beeper2 = new Runnable() {
           public void run() {
               (new Thread(beeper)).start();
           }
       };
       final ScheduledFuture<?> beeperHandle =       scheduler.scheduleAtFixedRate(beeper2, 1, 1, SECONDS);
    }
 }

What about this? 那这个呢?

 public static void main (String [] args) throws InterruptedException{

ExecutorService executorService =
        Executors.newFixedThreadPool(10);

while (true){
    executorService.submit(new Runnable() {
        @Override
        public void run() {
            // do your work here..
            System.out.println("Executed!");    

        }});
    Thread.sleep(1000);
    }       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM