编写线程类DelayPrintThread标题太长,剩下的在内容里。

编写线程类DelayPrintThread,构造方法中给出线程号,在线程体中产生一个1-10之间的随机数,使得线程体每休眠此随机数时间就打印输出线程号和休眠时间;另外编写应用DelayPrintThread类的Java应用程序TwoThread.java,在main()方法中创建两个线程,并应用sleep()控制主应用程序延迟一段时间。

求相关代码,我可以自己运行调试,谢谢大家!如果有运行结果也可以麻烦拍个照,我好对比一下。

参考:

package T13;

/**
 * Runnable实现线程特点:
 *     1.对象可以自由地继承自另一个类;解决了java只支持单继承的特点
 *     2.同一个runnable对象可以传递给多个线程,可以实现资源共享
 *     3.减小创建新线程实例所需的可观内存和cpu时间
 * 
 * 
 *     非运行状态的5种情况
 *     1.sleep;时间到了自动恢复执行;
 *     2.yield让步,
 *     3.suspend暂停,resume恢复运行
 *     4.wait等待,notify(),notifyall恢复运行
 *     5.I/O阻塞
 * 
 * */
public class RunnableDemo1 implements Runnable{

    @Override
    public void run() {
        for(int i=0;i<100;i++){
            //Thread.currentThread()获取当前线程
            System.out.println(Thread.currentThread().getName() +":i="+i);
            //休眠:单位是毫秒
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("休息了10毫秒,好爽。。。。。。。。");
            }
        }
    }

    public static void main(String[] args) {
        RunnableDemo1 r =  new RunnableDemo1();
        Thread thread1 = new Thread(r);
        thread1.setPriority(1);
        Thread thread2 = new Thread(r,"线程2");
        System.out.println("线程2的优先级:"+thread2.getPriority());
        Thread thread3 = new Thread(r);
        thread3.setPriority(8);
        thread1.setName("线程1");
        thread1.start();
        thread2.start();
        thread3.start();
        thread3.interrupt();
        
        
    }
}