How to implement simple threading with a fixed number of worker threads

后端 未结 7 897
渐次进展
渐次进展 2020-12-04 06:49

I\'m looking for the simplest, most straightforward way to implement the following:

  • The main program instantiates worker threads to do a task.
  • Only
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 07:22

    Executors.newFixedThreadPool(int)

    Executor executor = Executors.newFixedThreadPool(n);
    
    Runnable runnable = new Runnable() {
     public void run() {
      // do your thing here
     }
    }
    
    executor.execute(runnable);
    

提交回复
热议问题