1.什么是并行和并发

要想学习多线程,必须先理解什么是并发与并行

并行:指两个或多个事件在同一时刻发生(同时发生)

并发:指两个或多个事件在同一时间段发生

多线程学习一

2.什么是进程,线程

进程:进程是正在运行的程序的实例,进程是线程的容器,即一个进程中可以开启多个线程。

多线程学习一

线程:线程是进程内部的一个独立执行单元,一个进程可以同时并发运行多个线程

多线程学习一

多线程:多个线程并发执行。

3.线程的创建

Java中线程有四种创建方式

继承Thread类

实现Runnable接口

实现Callable接口

线程池

3.1继承Thread类

3.1.1创建自定义线程

public class MyThread extends Thread {
    public void run(){
        for (int i = 0; i < 10; i++) {
            System.out.println("我的进程执行力"+new Date().getTime());
        }
    }
}

3.1.2创建测试类

package com.multithread.demos;

import com.multithread.thread.MyThread;

import java.util.Date;

public class ThreadCreateDemo {
    public static void main(String[] args){
        //1.创建自定义线程
        MyThread thread = new MyThread();
        thread.start();
        //2.主线程循环打印
        for (int i=0; i<10; i++){
            System.out.println("main主线程正在执行:"+new Date().getTime());
        }
    }
}

3.2实现Runnale接口

3.2.1创建自定义类实现Runnale接口

 

public class MyRunable implements Runnable {
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("runable执行的时间"+new Date().getTime() + ";执行的次数" + i);
        }
    }
}

3.2.2测试

package com.multithread.demos;

import com.multithread.thread.MyRunable;
import com.multithread.thread.MyThread;

import java.util.Date;

public class ThreadCreateDemo {
    public static void main(String[] args){
        //1.创建自定义线程
        Thread thread = new Thread(new MyRunable());
        thread.start();
        //2.主线程循环打印
        for (int i=0; i<10; i++){
            System.out.println("main主线程正在执行:"+new Date().getTime());
        }
    }
}

3.3实现Callable接口

3.3.1FutureTask介绍

Callable需要使用FutureTask类帮助执行,FutureTask类结构如下:

多线程学习一

future接口

判断任务是否完成:isDone()

能够中断任务:cancel()

能够获取任务执行结果:get()

3.3.2创建自定义类实现Callable接口

public class MyCallable implements Callable<String> {
    public String call() throws Exception {
        for (int i = 0; i < 10; i++) {
            System.out.println("执行的时间:" + new Date().getTime() + ";执行的次数" + i);
        }
        return "MyCallable执行完成";
    }
}

3.3.3创建测试类

import com.multithread.thread.MyCallable;
import com.multithread.thread.MyRunable;
import com.multithread.thread.MyThread;

import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadCreateDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask task = new FutureTask(new MyCallable());
        Thread thread = new Thread(task);
        thread.start();

        for (int i=0; i<10; i++){
            System.out.println("main主线程正在执行:"+new Date().getTime());
        }
        System.out.println(task.get());
    }
}

3.4线程池-Executor

3.4.1线程池线类关系图

多线程学习一

Executor接口:声明了execute(Runnable runnable)方法,执行任务代码

ExectorService接口:继承Exector接口,声明方法:submit,invokeAll,invokeAny以及shutDown等

 

AbstractExecutorService抽象类:

实现ExecutorService接口,基本实现ExecutorService中声明的所有方法

ScheduledExecutorService接口:

继承ExecutorService接口,声明定时执行任务方法

ThreadPoolExecutor类:

继承类AbstractExecutorService,实现execute、submit、shutdown、shutdownNow方法

ScheduledThreadPoolExecutor类:

继承ThreadPoolExecutor类,实现ScheduledExecutorService接口并实现其中的方法

Executors类:

提供快速创建线程池的方法

ExecutorService接口:

继承Executor接口,声明方法:submit、invokeAll、invokeAny以及shutDown等

AbstractExecutorService抽象类:

实现ExecutorService接口,基本实现ExecutorService中声明的所有方法

ScheduledExecutorService接口:

继承ExecutorService接口,声明定时执行任务方法

ThreadPoolExecutor类:

继承类AbstractExecutorService,实现execute、submit、shutdown、shutdownNow方法

ScheduledThreadPoolExecutor类:

继承ThreadPoolExecutor类,实现ScheduledExecutorService接口并实现其中的方法

Executors类:

提供快速创建线程池的方法

3.4.2创建自定义类实现Runnable接口

import java.util.Date;

public class MyRunable implements Runnable {
    public void run() {
        for (int i=0; i<10; i++){
            System.out.println("MyRunnable线程正在执行:"+new Date().getTime());
        }
    }
}

3.4.3创建测试类

 

import com.multithread.thread.MyCallable;
import com.multithread.thread.MyRunable;
import com.multithread.thread.MyThread;

import java.util.Date;
import java.util.concurrent.*;

public class ThreadCreateDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.使用Executors创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //2.通过线程池执行线程
        executorService.execute(new MyRunable());
        //3.主线程循环打印
        for (int i=0; i<10; i++){
            System.out.println("main主线程正在执行:"+new Date().getTime());
        }
    }
}

总结

1.1实现接口和继承Thread类比较

  1. 接口更适合多个相同的程序代码的线程去共享同一个资源。
  2. 接口可以避免java中的单继承的局限性。
  3. 接口代码可以被多个线程共享,代码和线程独立。
  4. 线程池只能放入实现Runable或Callable接口的线程,不能直接放入继承Thread的类。

在java中,每次程序运行至少启动2个线程。一个是main线程,一个是垃圾收集线程。

1.2Runnable和Callable接口的比较

相同点:

1.两者都是接口

2.两者都可以用来编写多线程程序

3.两者都需要调用Thread.start()启动线程

不同点:

1.实现Callable接口的线程能返回执行结果,而实现Runnable接口的线程不能返回结果

2.Callable接口的call()方法允许抛出异常,而Runnable接口的run()方法不允许抛出异常

3.实现Callable接口的线程可以调用Futrue.cancel取消执行,而实现Runnable接口的线程不能

 

注意点:

Callable接口支持返回执行结果,此时需要调用FutureTask.get()方法实现,此方法会阻塞主线程直到获取‘将来’结果;当不调用此方法时,主线程不会阻塞!

 

 

相关文章:

  • 2021-12-08
  • 2021-12-08
  • 2021-11-28
  • 2022-02-07
  • 2022-02-07
  • 2022-02-07
  • 2021-11-22
猜你喜欢
  • 2021-11-28
  • 2021-11-28
  • 2021-09-28
  • 2021-10-20
相关资源
相似解决方案