读书人

线程示范【3】

发布时间: 2012-10-27 10:42:26 作者: rapoo

线程示例【3】

package com.thread;/** * 2010-10-26 * 通过实现Runnable接口实现线程 * @author Administrator * */public class Demo3 {public static void main(String[] args) {Monkey monkey=new Monkey();//monkey.run();Thread t=new Thread(monkey);t.start();t.start();  //编译不会出错,但运行会出错,一个线程类只能启动一次}  }class Monkey implements Runnable{public void run() {int time=0;while(true){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Hello,World");time++;if(time==10){break;}}}}
?

读书人网 >编程

热点推荐