前言
我们在做WS服务器的时候,如果我们在接收消息的方法中对所有用户群发消息的时候,如果不用线程池发送,那么就相当于在该线程中串行的发送消息,如果用户较多,就会占用大量时间,这时就需要使用线程池。
但是直接使用线程池的话,如果同时有3个连接,我们创建的线程池的线程数量是5,那么在使用中就会占用15个线程,这就十分恐怖,实际使用中用户数非常多的情况会创建大量的线程,最终导致服务不可用。
所以我们就要设置一个全局线程池(公共线程池)来处理消息的发送。
正文
线程池配置类
ThreadToolConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor;
@Configuration @EnableAsync public class ThreadToolConfig { @Bean("msgThreadPool") public Executor msgThreadPool() { int cpuNum = Runtime.getRuntime().availableProcessors(); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(cpuNum); executor.setMaxPoolSize(cpuNum * 2); executor.setQueueCapacity(300); executor.setKeepAliveSeconds(60); executor.setThreadNamePrefix("pool-thread-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; } }
|
需要多线程调用方法对应的类
MutiThreadUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import org.yeauty.pojo.Session;
@Component @Slf4j public class MutiThreadUtil { @Async("msgThreadPool") public void sendMessageText(Session session, String msg){ log.error("线程名:" + Thread.currentThread().getName()); if(session!=null && session.isActive()){ session.sendText(msg); } } }
|
调用
1 2 3 4 5 6
| @Autowired private MutiThreadUtil mutiThreadUtil;
public void sendMessageText(){ mutiThreadUtil.sendMessageText("123"); }
|
注意
需要多线程调用的方法一定要放在单独的类中才能生效。