美文网首页
【Java】【Thread多线程】线程池

【Java】【Thread多线程】线程池

作者: JerichoPH | 来源:发表于2017-04-08 08:48 被阅读10次

    线程池

    1. 基本应用
      public class Demo {
          public static void main(String[] args) throws IOException, InterruptedException {
              ExecutorService pool = Executors.newFixedThreadPool(2);
              pool.submit(new MyRunnable());
              pool.submit(new MyRunnable());
              
              pool.shutdown(); // 关闭线程池
          }
      }
      
      class MyRunnable implements Runnable {
          @Override
          public void run() {
              for (int i = 0; i < 10; i++) {
                  System.out.println(Thread.currentThread().getName() + "....." + i);
              }
          }
      }
      

    相关文章

      网友评论

          本文标题:【Java】【Thread多线程】线程池

          本文链接:https://www.haomeiwen.com/subject/qdflattx.html