java创建线程的两种方式

1.Thread thread1 = new Thread(){   
2.            public void run(){   
3.                try {   
4.                    Thread.sleep(500);   
5.                } catch (InterruptedException e) {   
6.                    // TODO Auto-generated catch block   
7.                    e.printStackTrace();   
8.                }   
9.                while(true){   
10.                    System.out.println("1:"+Thread.currentThread().getName());   
11.                }   
12.            }   
13.        };   
14.        thread1.start();   
15.           
16.        Thread thread2 = new Thread(new Runnable(){   
17.  
18.            public void run() {   
19.                // TODO Auto-generated method stub   
20.                try {   
21.                    Thread.sleep(500);   
22.                } catch (InterruptedException e) {   
23.                    // TODO Auto-generated catch block   
24.                    e.printStackTrace();   
25.                }   
26.                while(true){   
27.                    System.out.println("2:"+Thread.currentThread().getName());   
28.                }   
29.                   
30.            }   
31.                   
32.        });   
33.        thread2.start();  

 

你可能感兴趣的:(java)