本帖最后由 张林敏 于 2013-5-4 22:31 编辑
- <P>
- public class ThreadMain {
- /**
- * @param args
- * @throws InterruptedException
- */
- public static void main(String[] args) throws InterruptedException {
- StringBuffer sb = new StringBuffer("Start:");
- Thread thread1 = new Thread1(sb);
- Thread thread2 = new Thread2(sb);
- thread1.start();
- thread2.start();
- System.out.println(sb);
- }
- }
- class Thread1 extends Thread {
- private StringBuffer sb;
- public Thread1(StringBuffer sb) {
- super();
- this.sb = sb;
- }
- @Override
- public void run() {
- sb.append("\n[Thread1]");
- for (int i = 0; i < 100; i++) {
- sb.append(" "+i+" ");
- }
- }
- }
- class Thread2 extends Thread {
- private StringBuffer sb;
- public Thread2(StringBuffer sb) {
- super();
- this.sb = sb;
- }
- @Override
- public void run() {
- sb.append("\n[Thread2] Hello 黑马训练营 !");
- }
- }</P>
复制代码 像在上述的代码 要怎样才能先运行完 线程 thread1 与线程thread2 后在调用打印输出 ?
|