关于Java计划线程生活的混乱



我有一种方法,在其中我启动线程并从方法返回..现在我不确定天气在完成其执行或IT后将继续在内存中继续存在

将发布内存。

这是我的方法:

@RequestMapping(value = "/testThread", method = RequestMethod.GET)
    public @ResponseBody JSON_Response testThread(
            @PathParam("name") String name) {
        JSON_Response response = null;
        try {
            System.out.println("name is"+name);
            response = new JSON_Response();
            response.setStatusCode(name);

            TestThread mst = new TestThread(name);
            mst.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

这是我的线程类

public class TestThread extends Thread{  

    public String inputJSON;
    public static int myCount = 0;

public TestThread(String inputJSON) {
        super();
        this.inputJSON = inputJSON;
    }

public void run(){
        try{
            System.out.println("Expl Thread: "+(++TestThread.myCount));
            Thread.sleep(100);

        } catch (InterruptedException iex) {
            System.out.println("Exception in thread: "+iex.getMessage());
        }
        finally{
            printname(inputJSON);
        }
}


public void printname(String name){
    System.out.println(name);
}
}

所以我想在完成任务后询问天气,它将发布内存,无论我的方法都会在完成之前返回。

是的,该线程将在完成后被销毁;资源将被发布。

当然,某人仍然可以对Thread对象进行引用(不在您的情况下),并且在清除参考之前,该对象将不为GC-ED。对象本身不是很重,但可以参考其他对象,例如通过ThreadLocal,这可能会保留更多的垃圾;这是班级卸载的问题。

最新更新