0

さて、誰かがアドレスでhttpリクエストを行うたびにスレッドを開始するJavaでWebアプリケーションを構築しようとしています。これは良い習慣ですか?うまくいく?長所または短所?作業は以下のとおりで、春の例を使用します。必要なスレッドを追加しました。

ps:これはTomcatで実行されています

> HomeController.java

    @Controller  public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );
        new Teste().start();
        return "home";
    }}class Teste extends Thread{

    @Override
    public void run() {

        while(true){
            System.out.println("im in thread");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    }

> web.xml

    <?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/n/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

> servlet-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.example.threadTestes" />



</beans:beans>
4

2 に答える 2

2

管理された環境でのスレッド化は、一般的に悪い考えです。誰かがリクエストを送信するたびに、JMSのようなある種の抽象化を使用してバックグラウンドハンドラーを開始してみませんか?そうすれば、アクティブなスレッドの数(jmsプールサイズ)を制御できます。

于 2012-12-26T15:30:25.663 に答える
1

すべてのリクエストに対してスレッドを開始することは、マネージまたはアンマネージに関係なく、どのアプリケーションでもひどい考えです。その理由の 1 つは、OS が新しいスレッドにスタックを割り当てるために使用できるメモリの量が限られていることです。制限されているだけでなく、ボックス上のすべてのプロセス間で共有されます (ただし、Java プロセスはその一部しか使用できない場合があります)。ここで、これらのスレッドがまったく終了しないか、新しい要求が着信し続けるのに十分な速さで終了しないと想像してください。OS は最終的に新しいスレッド用のスペースを使い果たします。OS とその設定に応じて、結果は、new Thread() の魅力的な OutOfMemoryError から、ボックス全体が明らかに応答しなくなるまで、さまざまです。これは、Java プロセスが使用可能なすべてのスペースを消費し、新しいスレッドをまったく作成できないためです。したがって: 1) リクエストの処理には、少なくともスレッド プール (Executor) を使用します。JMS は永続性が必要な場合のオプションですが、ここでもプールでリクエストを処理します。JMS から供給されるだけです。2) ある種のメンテナンス タスクでは、アプリの起動時に別のスレッドを起動できます。どちらの場合も、自分でクリーンアップし、シャットダウン中に追加のスレッドを停止します。

于 2012-12-26T18:11:02.987 に答える