Java多线程(一)-线程创建

一.线程是什么 

线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务

二.线程创建

方法一:继承thread类,重写run方法,调用start启动线程

public class Thread01 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("run"+i);
        }
    }
    public static void main(String[] args) {
        Thread01 t1 = new Thread01();
        t1.start();
        for (int i = 0; i < 50; i++) {
            System.out.println("main"+i);
        }
    }
}

方法二:实现Runnable接口,重写run方法,创建线程对象放作为参数调用thread的start方法(推荐使用,可避免单继承局限性

public class Thread02 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("run"+i);
        }
    }
    public static void main(String[] args) {
        Thread02 t2 = new Thread02();
        new Thread(t2).start();
        for (int i = 0; i < 100; i++) {
            System.out.println("main"+i);
        }
    }
}

方法三:实现Callable借口,重写call方法,创建对象,创建服务,提交执行,获取返回结果,关闭服务

import java.util.concurrent.*;

public class Thread03 implements Callable {

    public String name;

    public Thread03(String name) {
        this.name = name;
    }
    
    @Override
    public Boolean call() throws Exception {
        Thread.sleep(100);
        return true;
    }
    
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread03 t1 = new Thread03("a");
        Thread03 t2 = new Thread03("b");
        Thread03 t3 = new Thread03("c");

        // 创建执行服务
        ExecutorService s = Executors.newFixedThreadPool(3);

        // 提交执行服务
        Future f1 = s.submit(t1);
        Future f2 = s.submit(t2);
        Future f3 = s.submit(t3);

        // 获取返回值
        boolean b1 = f1.get();
        boolean b2 = f2.get();
        boolean b3 = f3.get();

        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);

        // 关闭服务
        s.shutdown();
    }
}

补充

  • 模拟Thread的静态代理模式

    public class StaticProxy {
        public static void main(String[] args) {
            new WeddingCompany(new You()).HappyMerry();
            new Thread(() -> System.out.println("as")).start();
        }
    }
    
    interface Merry{
        void HappyMerry();
    }
    
    // 真实对象
    class You implements Merry{
    
        @Override
        public void HappyMerry() {
            System.out.println("开始");
        }
    }
    
    //代理真实对象处理问题
    class WeddingCompany implements Merry{
    
        private Merry target;
    
        public WeddingCompany(Merry target) {
            this.target = target;
        }
    
        private void before() {
            System.out.println("前");
        }
    
        @Override
        public void HappyMerry() {
            before();
            this.target.HappyMerry();
            after();
        }
    
        private void after() {
            System.out.println("后");
        }
    }
  • lambda表达式:前提是一个函数式接口(只有一个方法),有多行代码时用花括号包裹,多个参数时,参数类型可同时去掉或不去掉

    public class Lamb {
        public static void main(String[] args) {
            Bird b = null;
            // 无参数类型
            // b = () -> System.out.println("run...");
            // b.fly();
    
            // 有参数类型
            // b = (String type) ->System.out.println(type+"run");
            // b.fly("麻雀");
    
            // 多参数类型
            b = (type1,type2) ->{
                System.out.println(type1+"run");
                System.out.println(type2+"run");
            };
            b.fly("麻雀","海鸥");
        }
    }
    
    interface Bird{
        void fly(String type1,String type2);
    }

 

posted @ 2021-10-01 20:34  酥炸小黄瓜  阅读(44)  评论(0)    收藏  举报