Java 在Spring中实现线程功能的最佳方法

Java 在Spring中实现线程功能的最佳方法,java,multithreading,spring,spring-mvc,quartz-scheduler,Java,Multithreading,Spring,Spring Mvc,Quartz Scheduler,我正在用Java和Spring3.2进行聊天项目 通常在Java中,我可以创建如下线程: public class Listener extends Thread{ public void run(){ while(true){ } } } 然后通过start()启动线程 但是在spring3.x中是否有任何特殊的类或特殊的方式来实现线程功能 我的要求: 我有2个电信域服务器。在我的应用程序中,我需要初始化服务器以创建协议 在服务器初始化

我正在用Java和Spring3.2进行聊天项目

通常在Java中,我可以创建如下线程:

public class Listener extends Thread{
    public void run(){
         while(true){

         }
    }
}
然后通过
start()
启动线程

但是在
spring3.x
中是否有任何特殊的类或特殊的方式来实现线程功能


我的要求: 我有2个电信域服务器。在我的应用程序中,我需要初始化服务器以创建协议

在服务器初始化之后,我需要启动两个线程来侦听来自电信域服务器的响应

我所做的工作如下:

public class Listener extends Thread{
    public void run(){

       while(true){
           if(ServerOne.protocol != null){
                Message events = ServerOne.protocol.receive();
                   //Others steps to display the message in the view
           }
       }

    }
}

是否可以使用
quartz
执行java线程功能

如果可能的话,哪一个更好?如果没有,原因是什么


希望我们的堆栈成员能提供更好的解决方案。

Spring的TaskExecutor是在托管环境中运行这些线程的好方法。您可以参考以获得一些示例。

Spring的TaskExecutor是在托管环境中运行这些线程的一种好方法。您可以参考一些示例。

Java提供了您所需要的。我建议不要手动使用线程。您应该始终使用一些为您管理线程的工具。请看一下这个:

Java提供了您所需要的。我建议不要手动使用线程。您应该始终使用一些为您管理线程的工具。请看一下这个:

您可以使用Spring的
您可以在配置文件中定义执行器

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" destroy- method="shutdown">
  <property name="corePoolSize" value="2" />
  <property name="maxPoolSize" value="2" />
  <property name="queueCapacity" value="10" />
</bean>

<task:annotation-driven executor="taskExecutor" />
现在,每次调用
doSomething
时,如果执行器运行的线程数小于
corePoolSize
则会创建一个新线程。一旦创建了
corePoolSize
线程数,仅当运行(非空闲)线程的corePoolSize多于
maxPoolSize
且线程队列已满时,对
doSomething
的每次后续调用都将创建一个新线程。有关池大小的更多信息,请参阅

注意:在使用@Async时,如果应用程序中没有提供CGLIB库,则可能会遇到以下异常

Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
为了克服这个问题而不必添加CGLIB依赖项,您可以创建一个接口
IListener
,其中包含doSomething(),然后让
侦听器
实现
IListener
您可以使用Spring的
您可以在配置文件中定义执行器

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" destroy- method="shutdown">
  <property name="corePoolSize" value="2" />
  <property name="maxPoolSize" value="2" />
  <property name="queueCapacity" value="10" />
</bean>

<task:annotation-driven executor="taskExecutor" />
现在,每次调用
doSomething
时,如果执行器运行的线程数小于
corePoolSize
则会创建一个新线程。一旦创建了
corePoolSize
线程数,仅当运行(非空闲)线程的corePoolSize
多于
maxPoolSize
且线程队列已满时,对
doSomething
的每次后续调用都将创建一个新线程。有关池大小的更多信息,请参阅

注意:在使用@Async时,如果应用程序中没有提供CGLIB库,则可能会遇到以下异常

Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.

为了克服这一问题而不必添加CGLIB依赖项,您可以创建一个包含doSomething()的接口
IListener
,然后让
Listener
实现
IListener
,从Spring 3开始,您可以使用@Schedule annotation:

@Service
public class MyTest {
  ...
  @Scheduled(fixedDelay = 10)
  public getCounter() {...}
}
在上下文文件中使用


请参阅本教程:

自Spring 3以来,您可以使用@Schedule annotation:

@Service
public class MyTest {
  ...
  @Scheduled(fixedDelay = 10)
  public getCounter() {...}
}
在上下文文件中使用


请参阅本教程:

您可以查看@Scheduledyou可以查看@Scheduledyou可以查看@ScheduledShall我可以用石英做这些吗?我可以用石英做这些吗?