(8)线程Thread演示

来源:互联网 发布:www.taobao.com淘宝网 编辑:程序博客网 时间:2024/05/16 09:02
package com.xuan.thread;/*多线程演示,是Threads子类*/public class Threads extends Thread{String mesg;int count;/*run用于输出信息,共有count次*/public void run(){while(count-->0){println(mesg);try{Thread.sleep(100);//100msec}catch(InterruptedException e){return ;}}println(mesg+" all done");}void println(String s){System.out.println(s);}/*构造一个Threads对象 * @param m要显示的信息 * @param n要显示的次数 * */public Threads(String m,int n){count=n;mesg=m;setName(m+" return Thread");}/*主程序,Thread类的测试程序*/public static void main(String[] args) {//可以写入:new Threads("Hello from X",10).run();//也可以写成:new Threads("Hello form Y",15).run();//但非多线程的!new Threads("Hello from X",10).start();new Threads("Hello from Y",15).start();}}

0 0
原创粉丝点击