zoukankan      html  css  js  c++  java
  • Java多线程和并发(二),Thread中的start和run的区别

    目录

    1.调用run方法

    2.调用start方法

    3.start和run的区别

    二、Thread中的startrun的区别

    1.调用run方法

    public class ThreadTest {
        private static void attack() {
            System.out.println("Current Thread is : " + Thread.currentThread().getName());
        }
    
        public static void main(String[] args) throws InterruptedException {
            Thread t = new Thread(){
                @Override
                public void run() {
                    attack();
                }
            };
            System.out.println("current main thread is : " + Thread.currentThread().getName());
            t.run();
        }

     显示线程只有一个,即main线程

    2.调用start方法

    public class ThreadTest {
        private static void attack() {
            System.out.println("Current Thread is : " + Thread.currentThread().getName());
        }
    
        public static void main(String[] args) throws InterruptedException {
            Thread t = new Thread(() -> attack());
            System.out.println("current main thread is : " + Thread.currentThread().getName());
            t.start();
        }
    }


     
    我们是用
    lambda表达式来重写的Thread类,这个时候就会创建一个新的线程

    3.startrun的区别

     

  • 相关阅读:
    Git-更新数据
    iOS开发-基本的网络知识
    iOS开发-单例模式
    iOS开发-多线程知识
    iOS开发-核心动画随笔
    iOS开发-关于网络状态的判断
    毕业设计--天气预报App
    iOS开发-UIColor转UIIamge方法
    iOS开发-用预处理指令代替注释
    JMS 消息服务
  • 原文地址:https://www.cnblogs.com/xzmxddx/p/10362802.html
Copyright © 2011-2022 走看看