zoukankan      html  css  js  c++  java
  • 多线程(一)线程创建的三种方式

    创建线程的三种方式:

    1.第一种方式继承Thread类,重写Thread类中的run方法,还需要调用start方法,start方法相当于通知CPU,线程已经就绪,CPU在合适的时间点调用该线程的run方法;我们程序中的main方法,我们称之为主线程。

    2.创建线程的第二种方式,实现Runnable接口,并重写run方法,创建实例之后,将该实例包装成Thread实例,继续调用start方法让线程就绪,因为Runnable是一个函数式接口,因此可以通过Lambda表达式,进行Runnable实例的创建;
    3.创建线程的第三种方式,实现Callable,重写该接口的call方法,call方法不同于run方法,run方法没有返回值,而call
    方法有返回值;
        第一步:创建Callable实例,重写call方法
        第二步:将Callable实例传入FutureTask构造器中,得到FutureTask实例
        第三步:创建Thread实例,将FutureTask实例传入Thread构造器中,再让线程就绪
      

    public class Test implements Callable<String>{
    
        @Override
        public String call() throws Exception {
            String str = "test";
            // TODO Auto-generated method stub
            return str;
        }
        
        public static void main(String[] args) {
            Test test = new Test();
            FutureTask<String> futureTask = new FutureTask<>(test);
            Thread thread = new Thread(futureTask);
            
            thread.start();
        }
         
    }

     
    总结,通过集成Thread创建线程与实现Runnable接口与实现Callable接口创建线程的区别:

        1、相对而言,通过继承Thread创建线程代码是最简单的;
        2、如果我们的类继承了Thread那就不能再去继承其他类,因为java是单继承,如果我们通过实现接口Runnable或者
            Callable来创建线程,我们的类还有去权力去继承其他类;
        3、实现Runnable或者Callable接口,可以让多个线程共享同一份资源
        4、当我们需要线程执行完毕之后有返回值|信息返回,那么需要实现Callable接口
           因为Callable接口中的call方法声明了返回值
          
    通过synchronized实现线程安全:
    1、方法使用synchronized修饰,只有一个线程可以进入方法体中,当该方法体中的程序执行完毕之后,其他线程才可以进入;
    2、需要同步的代码块使用synchronized同步,提供同步监视器

  • 相关阅读:
    UOJ #455 [UER #8]雪灾与外卖 (贪心、模拟费用流)
    Codeforces 482E ELCA (LCT)
    Codeforces 798D Mike and distribution (构造)
    AtCoder AGC017C Snuke and Spells
    HDU 6089 Rikka with Terrorist (线段树)
    HDU 6136 Death Podracing (堆)
    AtCoder AGC032D Rotation Sort (DP)
    jenkins+python+kubectl实现批量更新k8s镜像
    Linux 下载最新kubectl版本的命令:
    jenkins X 和k8s CI/CD
  • 原文地址:https://www.cnblogs.com/deepSleeping/p/10258680.html
Copyright © 2011-2022 走看看