子线程中用InvokeLater 回归主线程同步执行

import java.awt.EventQueue;
public class TestInvokeLater {
  public static class V extends Thread{
    int i;
    public V(int i){this.i = i;}
    public void run()
    {
      try{
        System.out.println("doing" + i);
        Thread.sleep(3000l);
        System.out.println("done" + i);
//        new V(10+i).start();  //本行为再新建子线程,下行为转为主线程同步
        EventQueue.invokeLater( new V(10+i));
      }
      catch (Exception e) {
        // TODO: handle exception
      }
    }
  }
     public static void main(String[] args) throws Exception{
         new V(1).start();
         new V(2).start();
         new V(3).start();
         Thread.sleep(5000l);
     }
 }

发表于 28/11/2012 , 02:34 于分类 .

reeoo.com - web design inspiration

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注