利用线程池及相关技术完成以下要求:
(1)一个线程任务是将26个大写字母(A~Z)拼接为一个字符串
(2)另一个线程任务是将26个小写字母(a~z)拼接为一个字符串
(3)在主线程中分别获取两个线程的拼接结果,将结果按照、AaBbCc。。。的属性进行重组为新的字符串,并打印输出.
//小写字母拼接
class Lower implements Runnable {
Object obj;
StringBuffer str = new StringBuffer("");
public Lower(Object obj) {
this.obj = obj;
}
@Override
public void run() {
for (int i = 0; i < 26; i++) {
synchronized (obj) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print((char) (97 + i));
obj.notify();
str.append((char) (97 + i));
}
}
}
}
//大写字母拼接
class Big implements Runnable {
Object obj;
StringBuffer str = new StringBuffer("");
public Big(Object obj) {
this.obj = obj;
}
@Override
public void run() {
for (int i = 0; i < 26; i++) {
synchronized (obj) {
System.out.print((char) (65 + i));
obj.notify();
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
str.append((char) (65 + i));
}
}
}
}
public class Work2 {
public static void main(String[] args) {
ExecutorService pool = Executors.newCachedThreadPool();
Object obj = new Object();
Lower lower = new Lower(obj);
Big big = new Big(obj);
pool.submit(lower);
pool.submit(big);
pool.shutdown();
}
}