2017-03-31 101 views
3

我正在爲MMO瀏覽器遊戲編寫服務器,並且我需要製作幾個線程。 他們會在一段時間的睡眠中運行。 使用這樣的彈簧線是好主意嗎?在web應用中的彈簧線程

@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 id="taskExecutor" 
     class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> 
    <property name="corePoolSize" value="5" /> 
    <property name="maxPoolSize" value="10" /> 
    <property name="WaitForTasksToCompleteOnShutdown" value="true" /> 
</bean> 

此外,線程也是以singleton啓動的,也被定義爲一個bean。

我的方法有什麼問題?

+0

您是否解決了您的問題?只是問你是否需要更多的幫助。 – kkflf

回答

2

您可以使用@Scheduled(fixedDelay = 5000)定期執行方法。請記得爲包含主要方法的類設置@EnableScheduling

@Scheduled註釋有兩個選項 - fixedDelayfixedRate

fixedDelay將在上次執行完成後延遲X毫秒的時間繼續執行您的方法。

fixedRate將在固定的日期持續執行您的方法。因此,無論最後一次執行完成,每X毫秒該方法都將被執行。

如果您想一次處理大量對象,也可以使用@Async。再一次,您需要使用主要方法將@EnableAsync添加到您的課程中。

//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配置。有關此方法的更多信息可以在下面的鏈接中找到

我建議你看一看:

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

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

+0

那麼我真的不創建線程?聽起來不錯。當我沒有主要方法(我沒有使用SpringBoot)時,我應該在單線程中添加@EnableAsync註釋,這是啓動這個線程? – Parys

+0

好的,我找到了。如果有人會尋找類似的東西:可以通過在XML中放入 Parys

0

您可以在使用的情況下,你@Async要調用一個單線程以編程方式(例如,您想發送50封郵件,併發送它們創建50個不同的線程,每個線程發送一條消息,然後等待所有線程結束),或@Scheduled讓方法/線程以固定速率運行(或之前執行結束後的一段時間)。

您可以關注https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support瞭解更多詳情。

@Service 
public class MyAsyncStuff { 
    @Async 
    public Future<String> callMe(String param) { 
     // do your work here...   
     return new AsyncResult<String>("Sent: "+param);   
    } 
} 

@Service 
public class MyWorker { 
    @Inject 
    MyAsyncStuff stuff; 

    public void invoker() { 
     List<Future<String>> futures = new Arraylist<>(); 
     for (int i=0; i<10; i++) { 
      futures.add(stuff.callMe("Invoked "+i)); 
     } 
     for (Future<String> fut : futures) { 
      try { 
       System.out.println(futures.get(); 
      } catch (Exception e) { 
      // Spock? Do something! 
      } 
     } 
    } 
} 
+0

好吧,如果我想讓單線程完成所有的工作而不是啓動不同的工作,我應該使用@Scheduled?那麼在單身人士中開始他們呢? – Parys

+0

這取決於你想完成什麼。你是否有一個你希望一次處理的對象的集合,那麼你需要使用@Async。如果您想要定期執行特定任務,那麼您想使用'@ scheduled'。您還可以組合這兩個註釋,以定期執行一次處理一組對象的任務。 – kkflf

+0

(at)計劃用於重複執行任務,例如如果你想在某個時間間隔檢查一些東西。當你想要時,你不能停下來或開始。如果你想啓動一個線程讓它在後臺創建一些東西,爲了不阻止執行(例如準備一個zip文件,然後在完成時通過郵件發送它),一個(at)異步就是你需要的。 PrintTask可能屬於第二種情況。 – Sampisa

相關問題