public class MultiThreadingTest { /**
* @param args
*/
public static void main(String[] args) {
Integer tag = 0;
// TODO Auto-generated method stub
synchronized (tag) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread() {
public void run() {
for (int j = 0; j < 100; j++)
System.out.println(j);
}
};
thread.start();
}
}
synchronized (tag) {
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
}
}}意图很明确,就是想让上面那部分执行完成后,再打印 最后面的信息。如何写代码?谢谢!
public static void main(String[] args)throws Exception {
List<Thread> list = new ArrayList<Thread>();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread() {
public void run() {
for (int j = 0; j < 100; j++)
System.out.println(j);
}
};
list.add(thread);
}
for(Thread t : list){
t.start();
t.join();//主線程等待t線程結束
}
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
}
public class MultiThreadingTest { public static int tag = 0;
public static void main(String[] args) {
try {
for (int i = 0; i < 100; i++) {
Thread thread = new Thread() {
public void run() {
for (int j = 0; j < 100; j++)
System.out.println(j);
MultiThreadingTest.tag++;
}
}; thread.start();
}
while(MultiThreadingTest.tag!=100) {
System.out.println("Hello~ I am waiting....");
Thread.currentThread().sleep(15);
}
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
} catch (Exception e) {System.out.println(e);}
}}
//sorry,这样就可以了
public static void main(String[] args)throws Exception {
List<Thread> list = new ArrayList<Thread>();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread() {
public void run() {
for (int j = 0; j < 10000; j++)
System.out.println(Thread.currentThread().getName()+"---->"+j);
}
};
list.add(thread);
thread.start();
}
for(Thread t : list){
t.join();
}
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
}
谢谢您的回答!好久不用Multi Threading了
连这么简单的也忘掉了。用这个了~!