• java 多线程 day01 创建线程


    线程,即代码的执行路径
    java 面向对象编程。所以线程在java中也是用对象来表示的,创建线程对象的类就是 Thread
    如下代码即开启一个新的线程,通过线程对象的start方法,即可启动线程

    Thread thread = new Thread();
    thread.start();

    线程,即代码的执行路径。线程执行的代码是哪些代码呢?

    public void run() {
    if (target != null) {
    target.run();
    }
    }
    通过源码我们会发现,线程对象执行的代码,就是对象中 run方法中的代码
    由于Thread类是sun公司封装好的类,我们不能直接改写Thread的run方法。
    那么我们只能创建一个Thread类的子类,在子类中覆写run方法。那么线程对象 运行的就是子类中的run方法了。

    以下代码实现 每隔500毫秒,打印一次线程对象名称,打印100次。
    其中
    sleep()是静态的方法,即是类调用的,所以在哪个线程调用了sleep方法代码,那么就是那个线程睡眠。
    currentThread()返回当前的线程对象,该方法是一个静态的方法, 注意: 在哪个线程执行了currentThread()方法,就返回那个线程 的对象。
    public static void main(String[] args) {
    Thread thread = new Thread(){
    @Override
    public void run(){
    for (int i = 0; i < 100; i++) {
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(" 1 -- " + Thread.currentThread().getName());
    System.out.println(" 2 -- " + this.getName());
    }
    }
    };
    thread.start();
    }


    查看thread类的源码:
    public class Thread implements Runnable{

    private Runnable target;

    public Thread(Runnable target) {
    init(null, target, "Thread-" + nextThreadNum(), 0);
    }

    private void init(ThreadGroup g, Runnable target, String name,long stackSize, AccessControlContext acc) {
    this.target = target;
    }

    public void run() {
    if (target != null) {
    target.run();
    }
    }
    }
    由源码可知道,当thread类的构造方法入参为 runnable对象的时候,调用线程对象的start方法,进而调用的run方法就是runnable对象的run方法
    public static void main(String[] args) {
         Thread thread2 = new Thread(new Runnable(){
    public void run() {
    for (int i = 0; i < 100; i++) {
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(" 3 -- " + Thread.currentThread().getName());
    // System.out.println(" 2 -- " + this.getName());
    }
    }
    });
    thread2.start();
    //这里 this.getName() 就会保错,因为 new Runnable()对象不是线程对象,没有 getName 方法

    }

    一般我们采用runnable的方式,创建线程。
       因为1:继承是但继承的,如果一个类继承了thread类就没法继承其他的类了
    因为2:更加体现面向对象-线程类负责启动线程/runnable实现类负责运行代码

    同时有runnble实现类做参数和thread类子类对象,执行哪个run方法? -----> 子类的方法
    public static void main(String[] args) {
          new Thread(
    new Runnable(){
    public void run() {
    while(true){
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("runnable :" + Thread.currentThread().getName());

    }
    }
    }
    ){
    public void run() {
    while(true){
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("thread :" + Thread.currentThread().getName());

    }
    }
    }.start();
    }

    //以上结构为 thread(runnable.run){run}
    }


  • 相关阅读:
    sap function 常用的一些系统函数
    sap ok code
    提高PHP代码质量36计
    sap links /sap 学习资源链接
    sap tips/ sap 小技巧
    php写导入,导出 mysql csv
    SAP Tables 表
    [C#] 处理 Json
    [Rootkit] 无痕 hook 硬件断点
    [Rootkit] dll 隐藏 VAD
  • 原文地址:https://www.cnblogs.com/ctaixw/p/7923941.html
Copyright © 2020-2023  润新知