实验七 Java多线程
一、实验目的:
熟悉利用Thread类建立多线程方法。
熟悉利用Thread接口建立多线程方法。
二、实验内容:
1. 阅读下列程序,分析并上机检验其功能。
class DelayThread exends Thread{
private static int count=0;
private int no;
private int delay;
public DelayThread(){
count++;
no=count;
}
public void run(){
try{
for (int i=0;i<10;i++){
delay=(int)(Math.random()*5000);
sleep(delay);
System.out.println(“Thread ”+no+” with a delay ”+delay);
}
}catch(InterruptedException e){}}}
public class MyThread{
public static void main(String args[]){
DelayThread thread1=new DelayThread();
DelayThread thread2=new DelayThread();
thread1.start();
thread2.start()