美文网首页java多线程
多线程-线程创建

多线程-线程创建

作者: 余生爱静 | 来源:发表于2021-04-14 00:00 被阅读0次

关于线程的创建,我们随便在百度上搜索一下,搜索出来的结果有三种创建方式、四种创建方式、六种创建方式的也会搜索到。


线程创建

那正确的有几种创建方式呢?来从官网源码寻找正确答案吧

There are two ways to create a new thread of execution

译文:

有两种方式去创建一个新的可执行的线程
 One is to declare a class to be a subclass of Thread. This
 subclass should override the run method of class
  Thread. An instance of the subclass can then be
 allocated and started. 

译文:

第一种方式是继承Thread类,并重写Thread的run方法。子类的实例将会被分配和启动。
public class ThreadExtents extends Thread{
    @Override
    public void run() {
        System.out.println("This is thread extents");
    }
}
The other way to create a thread is to declare a class that
 implements the Runnable interface. That class then
 implements the run method. An instance of the class can
 then be allocated, passed as an argument when creating
 Thread, and started.

译文:

另外一种创建新线程的方式是通过实现Runnable接口,并且实现Run方法。类的实例可以被分配,并且可以当做参数来创建一个新的线程。
public class ThreadImplRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println("This is thread implements");
    }
}

综上所述:

创建线程的方式只有两种,第一种是继承Thread类,重写run()方法;第二种便是实现Runnable接口。

相关文章

  • 多线程

    创建一个多线程 创建多线程-继承线程类 创建多线程-实现Runnable接口 创建多线程-匿名类code

  • 10.3多线程详解

    Java高级-多线程 多线程创建 多线程通讯 线程池 1.多线程创建 thread/runnable图:继承Thr...

  • Python 多线程笔记

    Python 多线程笔记 创建线程 1. 使用函数创建多线程 2. 使用类创建多线程 继承 Thread 类 重写...

  • iOS基础知识 (三)

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

  • 多线程 -- threading

    多线程模块 threading 创建多线程的两种方式:import threadingimport time 创建...

  • Python多线程

    目录:一、线程的创建二、多线程互斥锁三、线程间通信四、线程池 Python并发之多线程 一、线程的创建 单线程示例...

  • JAVA基础—创建线程的3种方式

    线程的生命周期(五种状态) 创建线程的三种方式对比 1. 创建多线程-继承Thread 运行结果 2. 创建多线程...

  • 2018-10-04

    多线程方法1 使用线程工厂创建线程

  • 多线程

    多线程 多线程:一个进程中开辟多条线程,同时完成不同的任务,就是多线程。 创建方式:pthread:POSIX线程...

  • android 多线程学习2:线程的创建与方法分析

    android 多线程学习1:一些基础android 多线程学习2:线程的创建与方法分析android 多线程学习...

网友评论

    本文标题:多线程-线程创建

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