码迷,mamicode.com
首页 > 编程语言 > 详细

Java多线程和并发(二),Thread中的start和run的区别

时间:2019-02-11 18:36:02      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:san   exception   att   over   mic   static   ack   info   font   

目录

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的区别

 技术图片

 

Java多线程和并发(二),Thread中的start和run的区别

标签:san   exception   att   over   mic   static   ack   info   font   

原文地址:https://www.cnblogs.com/xzmxddx/p/10362802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!