Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过ThreadManager从新线程返回值。createThreadForCurrentRequest(new Runnable()gae java_Java_Multithreading - Fatal编程技术网

如何通过ThreadManager从新线程返回值。createThreadForCurrentRequest(new Runnable()gae java

如何通过ThreadManager从新线程返回值。createThreadForCurrentRequest(new Runnable()gae java,java,multithreading,Java,Multithreading,有两个部分。我使用谷歌应用程序引擎java。 1,任务队列,启动2进程 2,使用ThreadManager.createThreadForCurrentRequest(new Runnable()的每个进程{ 2线 我希望根据线程设置“total_I”值(int I_from,int I_to)。 将值(1,2)和(3,4)传递给每个螺纹时,total_i的总和应为6和14。 但是这两个线程给了我相同的值14。我真的很困惑,需要帮助。 谢谢 //第1部分: 总计数=4;//临时设置 记录计数=2

有两个部分。我使用谷歌应用程序引擎java。 1,任务队列,启动2进程

2,使用ThreadManager.createThreadForCurrentRequest(new Runnable()的每个进程{ 2线

我希望根据线程设置“total_I”值(int I_from,int I_to)。 将值(1,2)和(3,4)传递给每个螺纹时,total_i的总和应为6和14。 但是这两个线程给了我相同的值14。我真的很困惑,需要帮助。 谢谢

//第1部分:
总计数=4;//临时设置
记录计数=2;
Queue Queue=QueueFactory.getDefaultQueue();
//排队分段读取 详细数据,每次30个

对于(int i=1;i我测试过,它从(1,2)返回6。调试代码谢谢。但是当我测试2个线程时,给我相同的值,6或14。右边第一个线程应该是6,第二个14。请再次检查。感谢System.out.println(“线程处理后,total_i=“+total_i”);我仍然得到输出:线程处理后,线程处理后的total_I=14,System.out.println的total_I=14(“线程处理后,total_I=“+total_I”);我仍然得到输出:在线程处理之后,total_I=14在线程处理之后,total_I=14从第一部分调用第二部分。需要帮助。是的,但是您正在用第二个线程覆盖
recordAddr[0]
recordAddr[1]
中的值。这个变量应该是
TodayDetail()
方法的本地变量。
//part 1 : 

total_count = 4; // temp set
record_count= 2;
Queue queue = QueueFactory.getDefaultQueue();
// queue 分段读取 detail data, 每次30个
for (int i = 1; i <= total_count; i += record_count) {
  code_from = String.valueOf(i);
  code_to = String.valueOf(i + record_count - 1);
      queue.add(TaskOptions.Builder.withUrl("/test_DetailDown").method(TaskOptions.Method.GET).param("from", code_from).param("to", code_to));

}

//part 2:

    @SuppressWarnings("serial")
    public class test_DetailDown extends HttpServlet {
      AtomicInteger counter = new AtomicInteger();
      final static int ThreadCount = 2;
      int[] recordArr = new int[ThreadCount];

      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, DeadlineExceededException {

        resp.getWriter().println("start");
        // this is called by "Queue_Detail_Down" from Queue with parameter : from,
        // to
        String code_from = req.getParameter("from");
        String code_to = req.getParameter("to");

        TodayDetail(Integer.valueOf(code_from), Integer.valueOf(code_to)); 
        // process to down data
      }

      // this is the process to get today datail
      private void TodayDetail(int code_from, int code_to) {

        int total_i = 0;

        // int record_count = 2;

        // loop symbol to get detail data from stock.zaobao
        for (int i = 0; i < ThreadCount; i++) {

          final int i_from = code_from; // pass parameter
          final int i_to = code_to;
          // thread
          Thread thread = ThreadManager.createThreadForCurrentRequest(new Runnable() {
            public void run() {
              counter.incrementAndGet();
              get_detail_data(i_from, i_to); // down web data
              counter.decrementAndGet();
            }
          });
          thread.start(); // end thread process
        }
        // wait all thread
        while (true) {
          int num = counter.get();
          if (num <= 0)
            break;
          try {
            Thread.sleep(300);
          } catch (InterruptedException ex) {
          }
        }
        // save data
        for (int i = 0; i < ThreadCount; i++) {
          total_i = total_i + recordArr[i];
        }
        System.out.println("after Thread process, total_i =" + total_i);
        log("end of program");
      }

      // core process, get data
      private void get_detail_data(int i_from, int i_to) {

        for (int i = 0; i < ThreadCount; i++) {
          recordArr[i] = i_from + i_to;
        }

      }
    }