Java多线程实例:输出线程名20次

9、编写一个 Java 程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共 输出20次。

//programme name TestThread;

//声明一个子线程类Threaddemo;

class ThreadDemo extends Thread{ public ThreadDemo(String str){ super(str);

}

public void run(){

for(int i=0;i<20;i++){

System.out.print(""+this.getName());

Try{

Sleep(300);

}catch(InterruptedException e){

System.out.println(e.getMessage();

Return;

}

}

System.out.println(" /end");

}

public class TestThread {

public static void main(String args\[\]){

ThreadDemo thread1=new ThreadDemo("T1");

ThreadDemo thread2=new ThreadDemo("T2");

ThreadDemo thread3=new ThreadDemo("T3");

thread1.start();

thread2.start();

thread3.start();

相关推荐
学长毕设5 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
C^h5 小时前
c++ string
开发语言·c++
raindayinrain5 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
学逆向的5 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao6 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
zzu123zsw6 小时前
从臃肿的模板调用到声明式契约:深入 UniHttp 动态代理与多渠道生命周期扩展
java·spring boot·microsoft·unihttp
学逆向的6 小时前
C 局部静态变量
c语言·开发语言·网络安全
大都督会赢的7 小时前
C++第一节课
开发语言·c++
“AI国潮设计-小江”7 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
源码集结号7 小时前
云诊所门诊系统源码,基于SpringBoot+Vue.js架构,支持多租户设计与Docker容器化部署
java·spring boot·源码·医院信息系统·门诊系统·诊所系统