Web应用程序中的Spring线程 [英] Spring threads in web app

查看:180
本文介绍了Web应用程序中的Spring线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为MMO浏览器游戏编写服务器,我需要制作一些线程。
他们将一直运行一段时间。
使用这样的弹簧线是不是一个好主意?

I am writing server for MMO browser game, and I need to make few threads. They will be running all the time with some sleep time. Is it good idea, to use spring threads like this?

@Component
@Scope("prototype")
public class PrintTask2 implements Runnable{

String name;

public void setName(String name){
    this.name = name;
}

@Override
public void run() {

    System.out.println(name + " is running");

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println(name + " is running");

}

}

将任务执行器实现为bean?

with task executor implemented as bean?

<bean id="taskExecutor"
       class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="5" />
    <property name="maxPoolSize" value="10" />
    <property name="WaitForTasksToCompleteOnShutdown" value="true" />
</bean>

此外,线程以单例也启动,也被定义为bean。

Also, threads are started in singleton also defined as a bean.

我的方法有什么问题?

推荐答案

你可以使用 @ Scheduled(fixedDelay = 5000)定期执行方法。请记住为包含main方法的类设置 @EnableScheduling

You can use @Scheduled(fixedDelay = 5000) to execute a method periodic. Remember to set @EnableScheduling for class containing your main method.

<$ c $有两个选项c> @Scheduled 注释 - fixedDelay fixedRate

fixedDelay 将在上次执行完成后延迟X毫秒继续执行您的方法。

fixedDelay will continuously execute your method with a delay of X miliseconds after the last execution has finished.

fixedRate 将在固定日期持续执行您的方法。因此,无论最后一次执行是否完成,每X毫秒都将执行此方法。

fixedRate will continuously execute your method with at a fixed date. So every X milisecond this method will be executed regardless if the last execution has finished.

您还可以使用 @Async 如果你想一次处理一堆对象。再次,您需要使用main方法将 @EnableAsync 添加到您的班级。

You can also use @Async if you want to process a bunch of objects all at once. Once again, you need to add @EnableAsync to your class with your main method.

示例

//Remember to set @EnableScheduling
//in the class containing your main method
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

@Component
public class ScheduledTasks {

List<YourObject> myObjects;

    //This method will run every 5 second.
    @Scheduled(fixedDelay = 5000)
    public void yourMethodName() {
        //This will process all of your objects all at once using treads
        for(YourObject yo : myObjects){
            yo.process();
        }
    }
}

public class YourObject {

    Integer someTest = 0;

    @Async
    public void process() {
       someTest++;
    }
}

奖金
您可以通过扩展 AsyncConfigurerSupport 并覆盖 getAsyncExecutor 来删除池大小的XML配置。有关此方法的更多信息,请访问以下链接

Bonus You can get rid of your XML configuration for the pool size by extending AsyncConfigurerSupport and override getAsyncExecutor. More information about this approach can be found at the below links

我建议您查看:

https://spring.io/guides/gs/scheduling-tasks/

https:// spring。 io / guides / gs / async-method /

这篇关于Web应用程序中的Spring线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆