Java一个多线程的例子

 1 class SimpleThread extends Thread{
 2     public SimpleThread(String str){
 3         super(str);
 4     }
 5     
 6     public void run(){            //重写run
 7         for(int i=0; i<10; i++)
 8         {
 9             System.out.println(i + " "+ getName());
10             
11             try{
12                 sleep((int) (Math.random() * 1000)); //线程睡眠,让其他可运行态的程序执行
13             }catch(Exception e){
14                 
15             }
16                 
17         }
18         System.out.println("Done!" + getName());
19     }
20 }
21 
22 public class TwoThreads{
23     public static void main(String[] args){
24         new SimpleThread("First").start();      //线程1
25         new SimpleThread("Second").start();     //线程2
26         }
27 }
posted on 2012-11-21 10:13  ocr  阅读(258)  评论(0编辑  收藏  举报