从runnable任务调用外部方法。

时间:2015-10-08 06:01:31

标签: java multithreading

我有waitandsweep方法,我在其中创建一个可运行的任务并使用threadpoolexecutor安排它。从runnable任务我调用外部方法isSliceDone(),虽然值为true,但它并不尊重它。这意味着如果是部分,控制将转到其他地方。我认为这里有一些同步问题。有人可以帮忙吗?

    protected void waitAndSweep(final String symbol) {
        try {

          runnable = new Runnable() {
            @Override
            public void run() {
              try {

              if (isSliceDone()) {
                  long timeSinceLastSliceQtySent = lastSliceSentTime == 0 ? getInterval() : System.currentTimeMillis() - lastSliceSentTime;
                  long waitTime = timeSinceLastSliceQtySent >= getInterval() ? 0 : getInterval() - timeSinceLastSliceQtySent;

                  if (waitTime > 0) {
                    logTradeEvent("waitAndSweep", symbol, "waittime: " + waitTime);
                    Thread.sleep(waitTime);
                  }
                } else if (sweepInterval > 0) {
// It always comes here eventhough isSliceDone is true.
                  logTradeEvent("waitAndSweep", symbol, "sweepInterval: " + sweepInterval);
                  Thread.sleep(sweepInterval);
                }

                callSweep(symbol);
              } catch (Exception e) {

              }
            }
          };

          Future<?> self = threadPoolExecutor.submit(runnable);

        } catch (Exception e) {

        }
      }



 private boolean isSliceDone() {

    synchronized (this) {
      double sliceQuantity = getSliceQuantity();

      if (Quant.equals(sentSliceQty % sliceQuantity, 0.0) && Quant.equals(executedSliceQty % sliceQuantity, 0.0)) {

        return Quant.greater(sentSliceQty, executedSliceQty) ? false : true;
      }
      return false;
    }

  }

0 个答案:

没有答案