首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java创设线程的两种方式

2012-08-22 
java创建线程的两种方式1.Thread thread1 new Thread(){2.public void run(){3.try {4.Thread.sleep(500

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();  

?

热点排行