java
package hu;
import java.io.IOException;
import java.util.Scanner ;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class 软件1901学号193401040108刘宗佑作业1 {
static class MusicThread extends Thread{ //MusicThread是线程子类
public void run() { //子类必须重写run()方法
for(int i = 0 ;i < 5;i++)
{System.out.println(“听音乐”+i);}
try {
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(“睡了”);
IOException e = new IOException();
throw e;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println(“睡醒了”);
}
}}
public static void main(String arg[]) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
for(int i=0;i<3;i++) {
MusicThread t = new MusicThread(); //创建线程子类的实例
executor.execute(t);
}
executor.shutdown();
}
}