zoukankan      html  css  js  c++  java
  • Thread.run方法是同步方法

    Thread.run方法是同步方法,

    线程:

    package com.stono.thread.page005;
    
    public class MyThread extends Thread {
    
        @Override
        public void run() {
            super.run();
            System.out.println("MyThread");
        }
    }

    调用:

    package com.stono.thread.page005;
    
    public class Run {
    
        public static void main(String[] args) {
            MyThread myThread = new MyThread();
    //        myThread.start(); // start是异步的,通常会在main的打印之后执行
    //        myThread.start(); // 多次调用start会有IllegalThreadStateException异常
            myThread.run();  // run是同步方法,会在main的打印之前执行
            System.out.println("运行结束!");
        }
    
    }

     修改线程类:

    package com.stono.thread.page005;
    
    public class MyThread extends Thread {
    
        @Override
        public void run() {
            super.run();
            System.out.println("MyThread:"+this.currentThread().getName());
        }
    }

    发现run方法其实是main Thread调用的,所以就是同步的方法;

  • 相关阅读:
    jQuery选择器之层级选择器
    信息滚动制作
    scrollTop、offsetTop、clientTop
    模电GG
    matlab求解线性方程组
    NWERC2016I
    WEB开发资料集散
    NWERC2016H
    相量变换的性质
    GCJ2017R1C B. Parenting Partnering
  • 原文地址:https://www.cnblogs.com/stono/p/8615229.html
Copyright © 2011-2022 走看看