`
zhangsk
  • 浏览: 30443 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Java线程

阅读更多
<!----> 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 }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics