Java多线程-start和run方法的区别

调用线程的start方法是创建了新的线程,在新的线程中执行。
调用线程的run方法是在主线程中执行该方法,和调用普通方法一样

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.test.interview;
 
public class ThreadDemo {
    private static void attack() {
        System.out.println("fight");
        System.out.println("Attack current thread is :" + Thread.currentThread().getName());
    }
 
    public static void main(String[] args) {
        Thread t = new Thread() {
            @Override
            public void run() {
                attack();
            }
        };
        System.out.println("current thread is :" + Thread.currentThread().getName());
        t.start();
 
    }
}

 运行结果:

current thread is :main
fight
Attack current thread is :Thread-0

  

posted @   豆芽花花儿酱  阅读(397)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示