多线程课后作业

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)+"句话	");
            }
            else
            {
                System.out.print("这是第"+(i+1)+"句话	");
            }
            
            i++;
            
            try {
                Thread.sleep(100);              //100毫秒输出一个
                
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            
            if(i%5==0)                          //每5个换行
            {
                System.out.println("
");
            }
            
        }        
    }
    
    public static void main(String[] args) {
        
        TestThread t=new TestThread();
        
        t.start();        
    }    
}
View Code

原文地址:https://www.cnblogs.com/wangchuanqi/p/5280112.html