synchronized method or use spring @transactional?

后端 未结 6 617
你的背包
你的背包 2021-01-15 08:00

I have a method that will be used to send out email. i want to lock this method so only one thread can accses it per time and the rest pool up concurrently. should i synchro

6条回答
  •  灰色年华
    2021-01-15 08:28

    I'm not sure if it answers your question, but instead of creating a new thread for every mail and calling start on it you could have an Executor or ExecutorService as a member of your class, as an implementation you could use a ThreadPoolExecutor with a pool size of 1. Your sendMail method would then submit Runnables to the executor.

提交回复
热议问题