在Java中初始化线程时访问被拒绝错误

时间:2015-10-05 05:01:44

标签: java multithreading google-app-engine

我们尝试按照Java Thread Example?中的示例 - @Phil给出的示例。

下面的代码片段 - 在运行期间 - 给出错误:

HTTP Error 500 
access denied ("java.lang.RuntimePermission" "modifyThreadGroup")

at CreateThreads.<init>(CreateThreads.java:15)
at LbaApi.doPost(LbaApi.java:32)

当构造函数被调用&#34; public CreateThreads(ArrayList strList)&#34; 时,会发生错误。 CreateThreads是从doPost方法调用的 - 如下所示。我们尝试了很多选项,但无法解决错误。关于这个错误的任何建议 - 如果我们在这里遗漏了什么。谢谢,

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class LbaApi extends HttpServlet{

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

        PrintWriter writer = resp.getWriter();

        String threadName = "TNAME";
        String inputJson = req.getParameter("inputJson");
        String outputJson = "";

        ArrayList<String> strList = new ArrayList<String>();

        strList.add(threadName);
        strList.add(inputJson);
        strList.add(outputJson);

        CreateThreads myThread = new CreateThreads(strList); //Line 32          
        myThread.start();

        resp.setContentType("text/plain");

        outputJson = strList.get(2);        
        writer.println(outputJson);     
    }

}

CreateThreads代码段如下:

import java.util.ArrayList;

public class CreateThreads extends Thread {

    private ArrayList<String> strList;          
    private String threadName;

    public CreateThreads(ArrayList<String> strList) {//Line 15

        this.threadName     = strList.get(0);
        super.setName(this.threadName);
        this.strList        = strList;

    }

    @Override
    public void run() {

        if (this.threadName.equals("TNAME")){

            String outputJson = "{'TableA':{'field_A1':'A_11'}}";           
            this.strList.add(outputJson);           
        }
    }
}

1 个答案:

答案 0 :(得分:2)

为了在app引擎应用程序中使用线程,你可以生成一个documented in the official docs的线程:

  1. 创建一个实现Runnable
  2. 的类
  3. 使用com.google.appengine.api.ThreadManager.currentRequestThreadFactory()
  4. 创建一个主题工厂
  5. 使用newRequestThread()作为参数
  6. 致电上述工厂的Runnable

    请注意,除非您使用的是后端服务器,否则您的线程不会“超过”您当前的请求,使得线程基本上只对cron作业和您没有通常60秒超时但10分钟的任务有用

    我看到了@Andreas发布的URL,但我不相信在airospike上的回答是合适的。 aerospike.com上的问题是有人试图在应用引擎上运行通用Java EE应用程序,因此创建线程的方式与应用程序引擎环境不兼容。