您的位置:首页 > 其它

多线程课后作业

2016-03-15 16:48 351 查看
P364--1

尝试定义一个继承Thread类的类,并覆盖run()方法,在run()方法中每隔100毫秒打印一句话

package org.hanqi.zwxx;

public class TestThread extends Thread {

public void run()
{
int i=0;

while(true)
{
if(i<9)
{
System.out.print("这是第0"+(i+1)+"句话\t");
}
else
{
System.out.print("这是第"+(i+1)+"句话\t");
}

i++;

try {
Thread.sleep(100);              //100毫秒输出一个

} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

if(i%5==0)                          //每5个换行
{
System.out.println("\n");
}

}
}

public static void main(String[] args) {

TestThread t=new TestThread();

t.start();
}
}


View Code



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: