多线程练习

1,实现Runnable接口开启线程


```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Richard
 * @date 2020/3/8 0008-9:43
 */
 public class myDemo01 implements Runnable{
     protected int countDown = 10;//Default
     private static int taskCount = 0;
     private final int id = taskCount++;
     public myDemo01(){}
     public myDemo01(int countDown){
         this.countDown = countDown;
     }
     public String status(){
         return "#"+id+"("+(countDown > 0 ? countDown : "LiftOff!")+").";
     }
    @Override
    public void run() {
         while (countDown-- > 0){
             System.out.println(status()+"  ");
             //Thread.yield();
         }
          }
    public static void main(String[] args) {
//        myDemo01 myDemo01 = new myDemo01(10);
//        Thread thread = new Thread(myDemo01);
//        thread.start();
//        com.tulun.src01.myDemo01 myDemo011 = new myDemo01(8);
//        Thread thread1 = new Thread(myDemo011);
//        thread1.start();
//        new Thread(new myDemo01(10)).start();
//        for (int i = 0; i < 5; i++) {
//            new Thread(new myDemo01(5)).start();
//            //System.out.println("waiting");
//
//        }
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        for (int i = 0; i < 5; i++) {
//            executorService.execute(new myDemo01(5));
//        }
//        executorService.shutdown();
        //可以控制线程数量
//        ExecutorService executorService = Executors.newFixedThreadPool(5);
//        for (int i = 0; i < 5; i++) {
//            executorService.execute(new myDemo01(8));
//        }
//        executorService.shutdown();
        //SingleThreadExecutor是线程数量为1的FixedThreadPool
        //如果向SingleThreadPool提交了多个任务,那么这些任务会排队,每个任务会在下一个任务执行之前运行结束
        //所有的任务使用相同的线程
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 5; i++) {
            executorService.execute(new myDemo01(8));
        }
        executorService.shutdown();
    }
}

2,实现Callable接口,并将结果收集

```java
import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * @author Richard
 * @date 2020/3/8 0008-11:00
 */


 public class myDemo02 implements Callable {

     private int id;
     public myDemo02(int id){
         this.id = id;
     }


    @Override
    public String call() throws Exception {
        return "Result of TaskWithResult"+id;
    }
}

class CallableDemo{
    public static void main(String[] args) {
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        ArrayList> futures = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            futures.add(executorService.submit(new myDemo02(i)));
//            futures.add(executorService.submit(new myDemo02(i)));
//        }
//        for (Future fs : futures) {
//            try {
//                System.out.println(fs.get());
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } catch (ExecutionException e) {
//                e.printStackTrace();
//            }
//            finally {
//                executorService.shutdown();
//            }
//        }
        ArrayList> futures = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            myDemo02 myDemo02 = new myDemo02(i);
            FutureTask stringFutureTask = new FutureTask<>(myDemo02);
            futures.add(stringFutureTask);
        }
        for (Future fs : futures) {
            try {
                System.out.println(fs.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        }
    }
}

你可能感兴趣的:(多线程练习)