实现二------------实现Runnbale接口

来源:互联网 发布:java date 减一个月 编辑:程序博客网 时间:2024/06/14 06:28
/*Runnable接口实现多线程,则不像继承Tread类那样可以直接使用Thread类中的name属性, * 需要在类中单独定义一个name属性以保存名称。 *  * */class ThreadExecDemo implements Runnable {//实现Runnbale接口private String name;//保存线程名称private int time;//保存线程的休眠时间public ThreadExecDemo(String name, int time) {this.name = name; //设置线程名称this.time = time;//设置休眠的时间}@Overridepublic void run() { //覆写run()方法// TODO Auto-generated method stubtry {Thread.sleep(this.time);//休眠指定的时间} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(this.name + "线程,休眠" + this.time + "毫秒。");}}public class ThreadExecRunnable {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubThreadExecDemo mt1 = new ThreadExecDemo("线程A", 10000);//定义线程对象,指定休眠时间ThreadExecDemo mt2 = new ThreadExecDemo("线程B", 20000);ThreadExecDemo mt3 = new ThreadExecDemo("线程C", 30000);new Thread(mt1).start();new Thread(mt2).start();new Thread(mt3).start();}}

原创粉丝点击