多线程

作者: AndyDennisRob | 来源:发表于2020-02-29 22:22 被阅读0次

我们都知道,多线程尤其重要。

本文假设你已经知道了线程的概念。
下面我们先来了解一下线程的创建。

线程的创建

有两种方法:

  • 创建一个继承类
  • 创建一个实现Runnable接口的类

这两个思路来创建线程。

先来看看第一种。

class MyThread extends Thread{
    public void run(){
        System.out.println("Thread!");
    }
}
public class Main{
    public static void main(String args[]){
        Thread t = new MyThread();
        t.start();
    }
}

思路:

  • 从 Thread 派生
  • 覆写 run() 方法
  • 创建 MyThread 实例
  • 调用 start() 启动线程

来看看第二种方法:

class MyTread implements Runnable{
    public void run(){
        System.out.println("Thread");
    }
}

public class Main{
    public static void main(String args[]){
        Runnable r = new MyThread();
        Thread t = new Thread(r);
        t.start();
    }
}

思路:

  • 实现 Runnable 接口
  • 覆写 run() 方法
  • 在 main() 方法中创建 Runnable 实例
  • 创建 Thread 实例,并传入 Runnable
  • 调用 start() 启动线程

相关文章

  • iOS多线程 NSOperation

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程 pthread、NSThread

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程: GCD

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程运用

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程基础

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • 多线程介绍

    一、进程与线程 进程介绍 线程介绍 线程的串行 二、多线程 多线程介绍 多线程原理 多线程的优缺点 多线程优点: ...

  • iOS进阶之多线程管理(GCD、RunLoop、pthread、

    深入理解RunLoopiOS多线程--彻底学会多线程之『GCD』iOS多线程--彻底学会多线程之『pthread、...

  • iOS多线程相关面试题

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • 多线程之--NSOperation

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • iOS多线程之--NSThread

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

网友评论

      本文标题:多线程

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