编程:利用线程池完成以下线程任务:
(1)一个线程任务打印输出100个“&”;一个线程任务打印输出100个“*”
(2)完成以上两个线程任务之后,继续完成一个线程任务打印输出100个“~”“;一个线程任务打印输出100个”#“
public static void main(String[] args) throws ExecutionException, InterruptedException {
//创建一个默认的线程池
ExecutorService pool = Executors.newFixedThreadPool(1);
Runnable thread1 = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.print("&");
}
}
};
Runnable thread2 = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.print("*");
}
}
};
Runnable thread3 = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.print("~");
}
}
};
Runnable thread4 = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.print("#");
}
}
};
pool.submit(thread1);
pool.submit(thread2);
pool.submit(thread3);
pool.submit(thread4);
pool.shutdown();
}