JAVA多线程学习笔记(一)Java线程额三种初始化方式

1.继承Thread

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class TestThread {

 

    public static void main(String[] args) {

        System.out.println("程序.........启动了");

        //继承Thread

        Thread1 thread1=new Thread1();

        thread1.start();//启动继承Thread类型线程

        System.out.println("程序.........结束了");

    }

 

    //继承Thread

    public static class Thread1 extends Thread {

        @Override

        public void run() {

            System.out.println("进入当前线程");

            System.out.println("当前线程ID " + Thread.currentThread().getId());

            System.out.println("结束当前线程");

        }

    }

}

2.实现Runnable接口

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public class TestThread {

 

    public static void main(String[] args) {

        System.out.println("程序.........启动了");

        //实现Runnable接口

        Runable1 runable1=new Runable1();

        new Thread(runable1).start();

        System.out.println("程序.........结束了");

    }

    //实现Runnable接口

    public static class Runable1 implements Runnable {

        @Override

        public void run() {

            System.out.println("进入当前线程");

            System.out.println("当前线程ID " + Thread.currentThread().getId());

            System.out.println("结束当前线程");

        }

    }

}

3.实现Callable接口+FutrueTask

callable方式不常用,但是相比于以上两种方式,可以拿到县城返回结果,可以处理异常。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

public class TestThreadPool {

    public static void main(String[] args) {

        List<String> totalList = new ArrayList<>();

        ExecutorService service = Executors.newFixedThreadPool(4);

        BlockingQueue<Future<List<String>>> queue = new LinkedBlockingDeque<>();

        Future<List<String>> future1 = service.submit(new ExecuteTask());

        Future<List<String>> future2 = service.submit(new ExecuteTask());

        Future<List<String>> future3 = service.submit(new ExecuteTask());

        Future<List<String>> future4 = service.submit(new ExecuteTask());

        queue.add(future1);

        queue.add(future2);

        queue.add(future3);

        queue.add(future4);

        for(int i=0;i<queue.size();i++) {

            try {

                List<String> sqlList = queue.take().get();

                totalList.addAll(sqlList);

            catch (InterruptedException | ExecutionException e) {

                e.printStackTrace();

            }

        }

        service.shutdown();

    }

}

 

//这里的返回类型List<String>是自定义的,可以是任意类型

class ExecuteTask implements Callable<List<String>>{

    public ExecuteTask(List<String> binList,List<String> locationList) {

    }

     

    public List<String> call() throws Exception{

        List<String> sqlList = new ArrayList<String>();

         

        return sqlList;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值