美文网首页
Java-创建线程的三种方式

Java-创建线程的三种方式

作者: 有腹肌的豌豆Z | 来源:发表于2020-09-02 08:38 被阅读0次

继承Thread

  • 1、定义一个类MyThread继承Thread,并重写run方法。
  • 2、将要执行的代码写在run方法中。
  • 3、创建该类的实例,并调用start()方法开启线程。
public class TestThread extends Thread {
    
    public static void main(String[] args) {
        //3、创建该类的实例,并调用start()方法开启线程。
        MyThread myThread = new MyThread();
        myThread.start();
    }
    
    //1、定义一个类MyThread继承Thread,并重写run方法。
    static class MyThread extends Thread {
        
        @Override 
        public void run() {
            //2、将执行的代码写在run方法中。
            for (int i = 0; i < 100; i++) {
                System.out.print("线程名字:" + Thread.currentThread().getName() + " i=" + i +"\n");
            }
        }
    }

}

实现Runnable接口

  • 1、定义一个类MyRunnable实现Runnable接口,并重写run方法。
  • 2、将要执行的代码写在run方法中。
  • 3、创建Thread对象, 传入MyRunnable的实例,并调用start()方法开启线程。
    代码如下:
public class TestThread extends Thread {

    public static void main(String[] args) {
        //3、创建Thread对象, 传入MyRunnable的实例,并调用start()方法开启线程。
        Thread thread = new Thread(new MyRunnable());
        thread.start();
    }

    //1、定义一个类MyRunnable实现Runnable接口,并重写run方法。
    static class MyRunnable implements Runnable {
        
        @Override        
        public void run() {
            //2、将执行的代码写在run方法中。
            for (int i = 0; i < 100; i++) {
                System.out.print("线程名字:" + Thread.currentThread().getName() + " i=" + i + "\n");
            }
        }
    }
}

实现Callable接口

public class TestThread extends Thread {

    public static void main(String[] args) {

        //3、创建线程池对象,调用submit()方法执行MyCallable任务,并返回Future对象
        ExecutorService pool = Executors.newSingleThreadExecutor();
        Future<Integer> f1 = pool.submit(new MyCallable());

        //4、调用Future对象的get()方法获取call()方法执行完后的值
        // 注意,如果调用f1.get()方法,会阻塞主线程
        try {
            System.out.print("sum=" + f1.get() + "\n");
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        //关闭线程池
        pool.shutdown();

        for (int i = 0; i < 100; i++) {
            System.out.print("我是主线程的 i=" + i);
        }
    }

    //1、自定义一个类MyCallable实现Callable接口,并重写call()方法
    public static class MyCallable implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {

            //2、将要执行的代码写在call()方法中
            int sum = 0;
            for (int i = 0; i <= 100; i++) {
                sum += i;
                Thread.sleep(100);
            }
            return sum;
        }
    }

}

创建线程的三种方式对比

继承Thread类与实现Runnable接口的区别

  • 我们都知道java支持单继承,多实现。实现Runnable接口还可以继承其他类,而使用继承Thread就不能继承其他类了。所以当你想创建一个线程又希望继承其他类的时候就该选择实现Runnable接口的方式。

实现Callable接口与Runnable接口的区别

  • Callable执行的方法是call() ,而Runnable执行的方法是run()。
  • call() 方法有返回值还能抛出异常, run() 方法则没有没有返回值,也不能抛出异常。

相关文章

  • iOS基础知识 (三)

    多线程 多线程创建方式 iOS创建多线程方式主要有NSThread、NSOperation、GCD,这三种方式创建...

  • Java-创建线程的三种方式

    继承Thread 1、定义一个类MyThread继承Thread,并重写run方法。 2、将要执行的代码写在run...

  • 多线程编程

    创建线程 创建线程的三种方式 创建方式Threadclass继承Thread类(重点)Runnable接口实现Ru...

  • 1 Java线程知识

    Java三种创建线程的方式:

  • 多线程 学习1

    一、创建线程方式 java创建线程的方式,主要有三种:类Thread、接口Runnable、接口Callable。...

  • Java多线程基础一

    多线程创建的三种方式 lambda

  • 线程池的使用入门

    在上一篇文章中,我们总结了三种创建线程的方式:《Java多线程基础——三种创建线程的方式》,然而在真实的开发中很少...

  • Java创建线程的三种方式及对比

    一.Java创建线程的三种方式 Java中创建线程主要有三种方式:1.继承Thread类2.实现Runnable接...

  • iOS 线程

    pthread NSThread 第一种创建方式 第二种创建方式 第三种创建线程的方式 NSThread线程的状态...

  • 线程

    java 中创建线程有哪几种方式? Java中创建线程主要有三种方式: 一、继承Thread类创建线程类 (1)定...

网友评论

      本文标题:Java-创建线程的三种方式

      本文链接:https://www.haomeiwen.com/subject/ifvmsktx.html