【注意】Win下的类名……

本文介绍了一个简单的Java多线程程序示例,展示了如何创建和启动多个线程,并让它们按指定时间间隔输出信息。该程序有助于理解Java中Thread类的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

由于Win下的文件名不区分大小写,所以如果同一个包中有相同的类(比如下例中的TestThread类和testthread),就无法通过编译。

软院的一个同学发来一个貌似无错的程序,跑了一下才发现原来如此……

  1. class TestThread extends Thread {
  2.     private String whoami;
  3.     private long delay;
  4.     //Our constructor to store the name (whoami) and time to sleep (delay)
  5.     public TestThread(String s, long d) {
  6.         whoami = s;
  7.         delay = d;
  8.     }
  9.     //Run - the thread method similar to main() When run is finished, the thread dies.
  10.     //Run is called ** the start() method of Thread
  11.     @Override
  12.     public void run() {
  13.         //Try to sleep for the specified time
  14.         try {
  15.             sleep(delay);
  16.         } catch (InterruptedException e) {
  17.         } //Now print out our name
  18.         System.out.println("Hello World!" + whoami + "" + delay);
  19.     }
  20. }
  21. /** * Multimtest. A simple multithread thest program */
  22. public class testthread {
  23.     public static void main(String args[]) {
  24.         TestThread t1, t2, t3; //Create our test threads
  25.         t1 = new TestThread("Thread1"1000);
  26.         t2 = new TestThread("Thread2"2000);
  27.         t3 = new TestThread("Thread3"3000);
  28.         //Start each of the threads
  29.         t1.start();
  30.         t2.start();
  31.         t3.start();
  32.     }
  33. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值