多线程练习

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<String> {

     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<Future<String>> 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<String> fs : futures) {
//            try {
//                System.out.println(fs.get());
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } catch (ExecutionException e) {
//                e.printStackTrace();
//            }
//            finally {
//                executorService.shutdown();
//            }
//        }
        ArrayList<Future<String>> futures = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            myDemo02 myDemo02 = new myDemo02(i);
            FutureTask<String> stringFutureTask = new FutureTask<>(myDemo02);
            futures.add(stringFutureTask);
        }
        for (Future<String> fs : futures) {
            try {
                System.out.println(fs.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值