栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 多线程的基本使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 多线程的基本使用

文章目录
  • 1. 多线程的概念
  • 2. 线程的实现方式
    • 2.1 继承Thread类实现多线程
    • 2.2 实现Runnable接口实现多线程
    • 2.3 实现Callable的多线程

1. 多线程的概念
  1. 在程序执行的时候,即使没有开启多线程,Java后台也有多个线程在运行,最基本的是主线程main,垃圾回收线程gc
  2. 线程的运行有调度器安排调度,与操作系统相关,其调度顺序不能人为干预
  3. 多个线程对同一个资源进行交互的时候,需要加入并发控制,否则会造成数据的不一致
  4. 多线程会带来额外的开销
2. 线程的实现方式
  • Java中多线程有三种实现方式,分别为:1. 继承Thread类, 2. 实现Runnable接口, 3. 实现callable接口
  • 对于直接继承Thread类的多线程,每一个线程操作的对象都是独立的,没有并发控制的问题;对于实现接口的多线程,需要在Thread对象中运行,属于静态代理的方式,其多个线程共同操作一个对象,涉及到并发控制的问题
2.1 继承Thread类实现多线程
public class ThreadTest {

    public static void main(String[] args) {

        // 1. Test multi-thread extends Thread Class
        MyThread1 myThread1 = new MyThread1();
        myThread1.start();

        MyThread1 myThread2 = new MyThread1();
        myThread2.start();
    }

}

class MyThread1 extends Thread{

    @Override
    public void run() {
        super.run();

        System.out.println("extends Thread class and execute it, the id is: " + this.getName());

    }
}

2.2 实现Runnable接口实现多线程
  • 实现接口的多线程需要在Thread的环境中运行
public class ThreadTestRunnable {

    public static void main(String[] args) {
        MyThread1 myThread1 = new MyThread1();

        // 1. 实现接口的多线程与继承的执行方式不同
        // 2. Thread 实现了 Runnable的代理,它完成了多线程执行的基础结果

        new Thread(myThread1, "Thread_one").start();
        new Thread(myThread1, "Thread_two").start();
    }

}

class MyThread2 implements Runnable{

    @Override
    public void run() {
        System.out.println("The Thread is running and it implements runnable port. The name of the Thread is " + Thread.currentThread().getName());
    }
}

2.3 实现Callable的多线程

实现Callable的多线程的处理过程如下:

  1. 实现callable接口并指定范型,该范型与call方法返回的类型一致

  1. 创建服务,指定线程池的数量
    ExecutorService eSer = Executors.newFixedThreadPool(3);
  2. 在服务中提交线程
    Future submit1 = eSer.submit(thread1);
  3. 获取线程的执行结果
    Boolean result1 = submit1.get();
  4. 关闭线程池服务
    eSer.shutdown();
import java.util.concurrent.*;

public class MainClass {

    public static void main(String[] args) {
        Callable thread1 = new MyThread();
        Callable thread2 = new MyThread();
        Callable thread3 = new MyThread();

        // 1. 创建服务,设置线程池
        ExecutorService eSer = Executors.newFixedThreadPool(3);

        // 2. callable的线程提交进行执行
        Future submit1 = eSer.submit(thread1);
        Future submit2 = eSer.submit(thread2);
        Future submit3  = eSer.submit(thread3);

        // 3. 获取线程执行的结果
        try {
            Boolean result1 = submit1.get();
            Boolean result2 = submit2.get();
            Boolean result3 = submit3.get();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        // 4. 关闭线程池服务
        eSer.shutdown();
    }

}

class MyThread implements Callable{

    @Override
    public Boolean call() throws Exception {
        for (int i = 0; i < 20; i++) {
            System.out.println( Thread.currentThread().getName() + " is running in " + i);
        }
        return true;
    }
}


关于线程的三种基本实现方式

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/310072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号