线程中开启3个子线程,分别实现打印字符串(1)"A","B", "C" (2)"AA","BB","CC"(3)"ABC"-->"CBA"-->"BAC"
2个回答
2014-07-18 · 知道合伙人数码行家
关注
展开全部
public class Demo {
//(1) "A","B", "C" (2) "AA","BB","CC" (3) "ABC"-->"CBA"-->"BAC"
public static void main(String[] args)throws Exception{
String str = "ABC";
getThread(str);
String[] arr1 = {"AA","BB","CC"};
getThread(arr1);
String[] arr2 = {"ABC","CBA","BAC"};
getThread(arr2);
}
private static<T> void getThread(T t) throws InterruptedException{
Thread thread = new Thread(){
public synchronized void run(){
if(t.getClass() == String.class){
for(int i = 0; i < t.toString().length(); i++){
System.out.print(t.toString().charAt(i)+" ");
}
}else if(t.getClass() == String[].class){
String[] arr = (String[]) t;
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i]+" ");
}
}
}
};
thread.start();
thread.join();
System.out.println();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询