Java线程

本文介绍了一个简单的Java多线程示例程序。该程序通过创建并启动四个独立的线程来展示多线程的基本概念。每个线程都会输出其名称,并在运行过程中穿插休眠指令,以模拟实际工作负载。

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

 1  package  org.zsk.error;
 2 
 3  public   class  MultiThreadDemo1_1 {
 4 
 5       public  MultiThreadDemo1_1() {
 6           //  TODO Auto-generated constructor stub
 7           new  NewThread( " 1 " );       
 8           new  NewThread( " 2 " );   
 9           new  NewThread( " 3 " );
10           new  NewThread( " 4 " );
11 
12          System.out.println( " main thread begin! " );
13           for  ( int  i = 0 ; i < 100 ; i ++ ){
14              System.out.println( "   -->   " );
15             
16          }
17          System.out.println( " main thread end; " );
18 
19      }
20 
21       /**
22       *  @param  args
23        */
24       public   static   void  main(String[] args) {
25           //  TODO Auto-generated method stub
26           new  MultiThreadDemo1_1();
27      }
28     
29       class  NewThread  implements  Runnable {
30          NewThread(String threadName){
31              name  =  threadName;
32              t  =   new  Thread( this , name);
33              t.start();
34              System.out.println( " new thread  "   +  name  +   " begin " );           
35          }
36         
37           public   void  run(){
38               try {
39                   for  ( int  i = 0 ; i < 100 ; i ++ ){
40                      System.out.println(name);
41                      Thread.sleep( 30 );
42                  }                   
43              }  catch  (InterruptedException e) {
44                  System.out.println( " thread  " + name + " error! " );
45              }
46                 
47              System.out.println( " thread  "   +  name  +   " end; " );
48          }
49         
50           private  String name;
51           private  Thread t;
52      }
53 
54  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值