【问题标题】:Adding Task to executor将任务添加到执行器
【发布时间】:2014-01-21 20:47:05
【问题描述】:

我已经编写了一个内部类 CreateRecord 用于创建一个 Record 实例。它稍后在调用 create 方法时作为任务提交给执行程序。 我是否必须每次都创建一个内部类来表示要执行的任务(例如 deleteRecord、updateRecord)。 任何人都可以提出更好的方法。

 ExecutorService exec=new ThreadPoolExecutor(corePoolSize,maxPoolSize,
         keepAlive,TimeUnit.MINUTES,new LinkedBlockingQueue<Runnable>());

    public void create(String str ) throws RemoteException
    {
      exec.execute(new CreateRecord(str));
    }

    class CreateRecord implements Runnable
    {
      private String Id;
      public  CreateRecord(String Id)
      {
        this.Id=Id;
      }

      @Override
      public void run() 
      {

        System.out.println("Record creation in progress of: "+Id+
              " by thread: "+Thread.currentThread().getName());
        Record rec=new Record(Id);
        map.put(Id, rec);
      }
    }

【问题讨论】:

    标签: java multithreading executorservice


    【解决方案1】:

    你可以只实例化一个Runnable 而不是声明一个新类。这些在 Java 中称为匿名内部类。

    public void create(final String id) throws RemoteException
    {
        exec.execute(new Runnable()
        {
            @Override
            public void run() 
            {
                System.out.println("Record creation in progress of: "+id+" by thread: "+Thread.currentThread().getName());
                Record rec=new Record(id);
                map.put(id, rec);
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

      ExecutorService 需要 Runnable 对象。 Runnable 对象必须是类的实例。

      两种选择:

      (1) 参数化您的Runnable 类,使其能够做不止一件事。但这违反了一些好的设计原则。

      (2) 使用匿名类:

      public void create(final String id) throws RemoteException {
          exec.execute(new Runnable() {
              @Override
              public void run() {
                  System.out.println("Record creation in progress of: "+id+" by thread: "+Thread.currentThread().getName());
                  Record rec = new Record(id);
                  map.put(id, rec);
              }
          });
      }
      

      请注意,id 和您想在 Runnable 之外使用的任何其他变量都必须声明为 final

      仅供参考,

      System.out.printf(
          "Record creation in progress of: %d by thread: %s%n",
          id,
          Thread.currentThread().getName()
      );
      

      更易读。

      【讨论】:

        【解决方案3】:

        有一个像你现在这样的类,比如Record,并在构造函数中指定一个额外的intString 参数,可以在run() 方法中使用来决定要做什么——即根据第二个参数的值更新、删除或添加记录。像这样的:

        class Record implements Runnable
        {
          private String id;
          private String operation;
          public  Record(String id, String operation)
          {
            this.id = id;
            this.operation = operation;
          }
        
          @Override
          public void run() 
          {
              if(operation.equals("update")) {
                  //code for updating
              } else if(operation.equals("insert")) {
                  System.out.println("Record creation in progress of: "+Id+
                          " by thread: "+Thread.currentThread().getName());
                    Record rec=new Record(id);
                    map.put(id, rec);
              } else {//if(operation.equals("delete"))
                  map.remove(id);
              }
        
          }
        }
        

        【讨论】:

        • 我不太喜欢 stringly typed command pattern 的想法。
        • 如果 Enum 是 UPDATE、INSERT 和 DELETE 的操作是 Enum 类型并使用 switch 语句会更好。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-26
        • 2021-05-31
        • 2018-03-02
        • 1970-01-01
        • 1970-01-01
        • 2019-09-10
        • 1970-01-01
        相关资源
        最近更新 更多