flysky7931
2008-12-22 09:54
浏览 323
已采纳

关于java线程突然死亡

郁闷了,线程无故自动终止,执行了finally代码段:
下面是线程的RUN方法:
[code="java"]public void run () {
// Output welcome message to the console
labels = ServerLabels.getInstance();
Log4jUtil.info ("------------------------------------------------------------------");
Log4jUtil.info (" 游 戏 平 台 ");
Log4jUtil.info ("------------------------------------------------------------------");
Log4jUtil.info (labels.get("author") + ": Bob Marks");
Log4jUtil.info (labels.get("version") + ": " + IJogre.VERSION);
Log4jUtil.info ("------------------------------------------------------------------");
Log4jUtil.info (labels.get("persistent.server.data") + ": " + dataConnection.getType());
Log4jUtil.info ("------------------------------------------------------------------");
Log4jUtil.info (labels.get ("games.being.served") + " (" + gameList.size() + "):");

    // Load the various games
    Log4jUtil.info (gameLoader);

    // End of welcome message
    Log4jUtil.info ("------------------------------------------------------------------\n");

    // Declare server socket to listen for client connections
    ServerSocket listenSocket = null;
    try {
        // Set up the server first of all
        listenSocket = new ServerSocket (serverPort);


        while (true) {
            // listen for and accept the connection
            Socket clientSocket = listenSocket.accept ();
            try{
                Log4jUtil.info( clientSocket+"  HASHCODE "+clientSocket.hashCode() );


                BufferedReader   br = new BufferedReader (new InputStreamReader (clientSocket.getInputStream(),"UTF-8"));
                PrintStream   pw = new PrintStream (clientSocket.getOutputStream(),true,"UTF-8");


//              BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//              PrintWriter pw = new PrintWriter(clientSocket.getOutputStream());
                String head=null;
                int count = 0;

                while(count<=10&&head==null){
                    if(br.ready()){
                        char[] by = new char[22];
                        br.read(by,0,22);
                        head = new String(by);
                    }else{
                        count++;
                        this.sleep(500);
                        Log4jUtil.info("连接层,流没有准备被读取,所以跳过,等待下次读取并累积读取次数+1:"+count);
                    }
                }

//              String head = br.readLine();
                if(head!=null&&!head.equals("")){
                    head = head.trim();
                    Log4jUtil.info("后台接收消息头:" + head + "&&&:&&&");
                    if(head.equals("<policy-file-request/>"))
                    {
                            pw.print(this.getXml() + "\0");
                            pw.flush();
                    }else if(head.equals("<policy-file-xxquest/>")){
                          @SuppressWarnings("unused")
                          ServerConnectionThread conn = new ServerConnectionThread (clientSocket,br,pw);
                          pw.println("<table><creatThread/></table>"+"\0");
                          pw.flush(); 
                          Log4jUtil.info("flash直连后台程序");
                    }else if(head.equals("<policy-file-xyquest/>")){   
                          @SuppressWarnings("unused")
                          ServerConnectionThread conn = new ServerConnectionThread (clientSocket,br,pw);
                          Log4jUtil.info("flash通过中间层转发连后台程序");
                      }
                }else{
                    Log4jUtil.info("连接服务器的时候由于读取到的字符为NULL而关闭对应的SOCKET");
                    clientSocket.close();
                }
            }catch(Exception e){
                //关闭连接
                if(clientSocket!=null){
                    clientSocket.close();
                }
                Log4jUtil.info("捕获异常了,准备对外抛出"+e.getMessage().toString());
                //抛出空指针异常,使其被外面的捕获,然后重新启动线程
                throw new NullPointerException();
            }
            // Try to connect client to this server

            //conn.setSendListener (sentListener);
            //conn.setRecieveListener (recieveListener);
        }
    }catch (BindException bindEx) {
        Log4jUtil.info ("jogre.server.already.running.on.port1: " + serverPort);
        //System.exit (0);
    }
    catch (Exception genEx) {
        Log4jUtil.info ("jogre.server.already.running.on.port2: " + serverPort);
        genEx.printStackTrace();
    }finally{
        // Close the server down again
        try {
            Log4jUtil.info  ("is over listenSocket close: " + serverPort);
            if (listenSocket != null)
                listenSocket.close();
            Log4jUtil.info  ("is run: " + serverPort);
            this.run();
        }
        catch (IOException ioEx) {
            Log4jUtil.info  ("listenSocket close exception: " + serverPort);
        }
    }
}[/code]

日志的信息如下
[code="java"]
[framework] 2008-12-21 18:32:06,921 [Thread-2] - Socket[addr=/10.10.10.1,port=2350,localport=1790] HASHCODE 16661568
[framework] 2008-12-21 18:32:06,921 [Thread-2] - 后台接收消息头:&&&:&&&
[framework] 2008-12-21 18:32:07,234 [Thread-2] - Socket[addr=/10.10.10.1,port=2351,localport=1790] HASHCODE 2554984
[framework] 2008-12-21 18:32:07,234 [Thread-2] - 后台接收消息头:&&&:&&&
[framework] 2008-12-21 18:32:07,859 [Thread-2] - flash直连后台程序
[framework] 2008-12-21 18:32:14,046 [Thread-2] - Socket[addr=/10.10.10.1,port=2353,localport=1790] HASHCODE 27504706
[framework] 2008-12-21 18:32:15,875 [Thread-2] - is over listenSocket close: 1790
[framework] 2008-12-21 18:32:15,875 [Thread-2] - is run: 1790
[framework] 2008-12-21 18:32:15,875 [Thread-2] - ------------------------------------------------------------------
[framework] 2008-12-21 18:32:16,218 [Thread-2] - 游 戏 平 台

[framework] 2008-12-21 18:32:16,218 [Thread-2] - ------------------------------------------------------------------
[framework] 2008-12-21 18:32:16,515 [Thread-2] - Author: Bob Marks
[framework] 2008-12-21 18:32:16,515 [Thread-2] - Version: alpha 0.2.3
[framework] 2008-12-21 18:32:16,828 [Thread-2] - ------------------------------------------------------------------
[framework] 2008-12-21 18:32:17,484 [Thread-2] - Persistent Server Data: database
[framework] 2008-12-21 18:32:17,484 [Thread-2] - ------------------------------------------------------------------
[framework] 2008-12-21 18:32:17,484 [Thread-2] - Games being served (1):
[framework] 2008-12-21 18:32:17,484 [Thread-2] - [controller] hrmajiang

[framework] 2008-12-21 18:32:17,781 [Thread-2] - ------------------------------------------------------------------

[framework] 2008-12-21 18:32:22,687 [Thread-2] - Socket[addr=/10.10.10.1,port=3330,localport=1790] HASHCODE 4172024
[framework] 2008-12-21 18:32:26,046 [Thread-2] - is over listenSocket close: 1790
[framework] 2008-12-21 18:32:26,359 [Thread-2] - is run: 1790
[framework] 2008-12-21 18:32:26,359 [Thread-2] - ------------------------------------------------------------------
[framework] 2008-12-21 18:32:26,656 [Thread-2] - 游 戏 平 台

[framework] 2008-12-21 18:32:26,656 [Thread-2] - ------------------------------------------------------------------
[framework] 2008-12-21 18:32:26,656 [Thread-2] - Author: Bob Marks
[framework] 2008-12-21 18:32:26,968 [Thread-2] - Version: alpha 0.2.3
[framework] 2008-12-21 18:32:27,281 [Thread-2] - ------------------------------------------------------------------
[framework] 2008-12-21 18:32:27,281 [Thread-2] - Persistent Server Data: database
[framework] 2008-12-21 18:32:27,281 [http-8080-Processor21] - Dispatch[/admin/loginAction] to method login returned an exception
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.extremecomponents.table.filter.AbstractExportFilter.doFilter(AbstractExportFilter.java:50)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:75)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)
[/code]

问题点是:
1.为什么线程会在无任何异常的情况下终止?
[framework] 2008-12-21 18:32:26,046 [Thread-2] - is over listenSocket close: 1790
2.如果使用tomcat5.5做服务器,在设置初始化内存大小和最大内存大小的时候是否有限制?比如不能大于2048MB
因为每次设置2048的时候TOMCAT都无法正常启动,设置成1536MB就可以了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 不良校长 2008-12-22 15:20
    已采纳

    看看这个: http://sdh5724.iteye.com/admin/blogs/283977
    另外, 在32位平台下, 建议1280M是最大的了. 因为JVM本身还需要不少内存.
    THREAD: 你可以把JVM的所有异常抓住, 包括Error, Throbale等等, 打印出来看看, 可能你有没有抓到的异常出现了.

    点赞 评论
  • iteye_15376 2008-12-23 12:51

    1.这个问题是每次都出现吗?是的话,debug一下就知道了。
    2.如果不是的话,需要捕获一个Throwable异常。看看异常到底是啥。
    3.这个问题应该跟你JVM的堆设置没啥关系。

    点赞 评论

相关推荐 更多相似问题