Java多线程学习1

java提供了丰富的多线程操作,直接使用Java.Lang.Concurrent包下的Api,即可开启使用Java提供的多线程功能。

1.基础篇。

               1.1 三种创建 多线程的方法。

                        1.直接通过继承Thread类,重写“Run()” 方法,直接调用其Start()方法即可开启并运行一个单独的线程。(不推荐,通过此方法可以显式的开启一个子线程任务,显式的通过非池化的技术开启多线程会导致线程使用的不可预见性,非常不利于管理)

System.out.println(Thread.currentThread().getName());
        new Thread(){
            @Override
            public void run() {
                System.out.println("子线程启动辣!!!");
                try {
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("当前线程为:" + Thread.currentThread().getName());
            }
        }.start();

2.实现 Runable接口,直接通过实现 Runnable接口的方法显式的创建一个子线程。 (不推荐,通过此方法可以显式的开启一个子线程任务,本质上和直接继承Thread区别不大)

 System.out.println(Thread.currentThread().getName());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("子线程启动辣!!!");
                try {
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("当前线程为:" + Thread.currentThread().getName());
            }
        };
        new Thread(runnable).start();

3.实现 Callable接口,直接通过实现 Callable接口的方法显式的创建一个子线程和Runnable区别在于是否有返回值。 (不推荐,通过此方法可以显式的开启一个子线程任务,本质上和直接继承Thread区别不大)

System.out.println(Thread.currentThread().getName());
        Callable callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("子线程启动辣!!!");
                try {
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("当前线程为:" + Thread.currentThread().getName());
                return "操作成功辣!!!";
            }
            };
        ExecutorService executorService = Executors.newFixedThreadPool(11);
        executorService.submit(callable);

1.2.使用线程池创建并使用多线程。

        1.使用Jdk提供的工具包Executors开启线程池(不推荐)

 System.out.println(Thread.currentThread().getName());
        ExecutorService executorService = Executors.newFixedThreadPool(22);
        executorService.submit(() -> {
            System.out.println("子线程启动辣!!!");
            try {
                TimeUnit.MILLISECONDS.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("当前线程为:" + Thread.currentThread().getName());
        });
        executorService.submit(() -> {
            System.out.println("子线程2启动辣!!!");
            try {
                TimeUnit.MILLISECONDS.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("当前线程为:" + Thread.currentThread().getName());
        });

2.使用ThreadPoolExecutor开启手动开启线程池并创建多线程(推荐),配合Jdk 8 提供的CompletableFuture异步任务,能够实现强大的多线程异步任务的能力。也是开发中常会使用的开发模式。可以使用该组件做定时任务,配合Stream实现并行流,实现各类多线程任务等……

System.out.println(Thread.currentThread().getName());
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                20
                , 100
                , 3000
                , TimeUnit.MILLISECONDS
                , new ArrayBlockingQueue<>(200)
                , Executors.defaultThreadFactory()
                , new ThreadPoolExecutor.AbortPolicy()
        );
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("执行中!!!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, threadPoolExecutor);
        completableFuture.thenRunAsync(() -> System.out.println("完成!!"),threadPoolExecutor);

 1.3.线程安全,锁,synchronized,ReentranLock,自旋锁,原子类,AQS,线程隔离,线程协商中断interrupt……

        1.锁,保证线程安全,实现共享数据同一时间只能一个线程访问,解决了并发修改的问题。底层原理,通过添加一个Monitor管程保证改类或对象线程访问的唯一性,synchronized本质上和ReentranLock一样。都是通过管程Monitor保证线程安全。弊端是悲观锁,导致性能大幅度下降。

        2.使用线程 安全的类,在一定程度上能够保证线程的安全,例如ConcurrentHashMap,HashTable就是线程安全的Map。使用线程安全的原子类,例如AtomicInteger就是线程安全的Integer类,内部通过 自旋的方式一定程度的保证线程的安全……

          未完待续……

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值