欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Runnable 博客分类: 基础知识

程序员文章站 2024-03-05 22:43:37
...

  1 import java.lang.Runnable;
  2 import java.lang.Thread;
  3 import java.util.concurrent.Executors;
  4 import java.util.concurrent.ExecutorService;
  5 import java.util.concurrent.Future;
  6 import java.util.concurrent.ExecutionException;
  7
  8 class runclass implements Runnable {
  9     public static long id = 0;
 10     
 11     public void run(){
 12         ++id;
 13         System.out.println(id);
 14     }
 15 }
 16
 17 class test {
 18     public static void main(String[] args) throws InterruptedException, ExecutionException {
 19         ExecutorService exec = Executors.newCachedThreadPool();
 20         Future result1 = exec.submit(new runclass());
 21         Future result2 = exec.submit(new runclass());
 22         while (!result1.isDone() || !result2.isDone()){
 23             Thread.sleep(1000);
 24             break;
 25         }
 26         exec.shutdown();
 27     }
 28 }
 29