是否可以实现具有线程作为观察者的可观察模式,如果是这样,这是一个好主意?

时间:2021-08-12 01:02:29

Im developing a system in java that will retrieve from a database emails waiting to be sent and it will load them in a temporary buffer. Whenever emails are detected on buffers the idea is to use the observer/observable pattern to notify the smtp servers. the point is to have each server running on a different thread and all threads are observing the buffers. if anyone can help me understand how to implemented i would appreciate it. I 've already used the observer pattern before but never with multithreading.

我在java中开发一个系统,它将从数据库中检索等待发送的电子邮件,并将它们加载到临时缓冲区中。每当在缓冲区上检测到电子邮件时,想法是使用observer / observable模式来通知smtp服务器。关键是让每个服务器在不同的线程上运行,所有线程都在观察缓冲区。如果有人可以帮助我了解如何实施我会很感激。我之前已经使用过观察者模式,但从未使用过多线程。

hope someone can help me since I've been searching a lot on the internet but i couldnt find any answers

希望有人可以帮助我,因为我一直在互联网上搜索,但我找不到任何答案

1 个解决方案

#1


0  

That's basically the idea of an ExecutorService. You create an executor service backed by one or several threads (a thread pool), and you submit tasks to this executor service. The tasks are stored in a queue, from which the thread(s) of the executor service takes the tasks it must execute. When the queue is empty, the thread(s) wait for a new task to appear in the queue.

这基本上是ExecutorService的想法。您创建由一个或多个线程(线程池)支持的执行程序服务,并将任务提交给此执行程序服务。任务存储在队列中,执行程序服务的线程从该队列中获取必须执行的任务。当队列为空时,线程等待新任务出现在队列中。

You could have a Map<SmtpServer, ExecutorService>, and submit all the mails you have found in the database to the appropriate executor service. Use Executors to create the appropriate implementation of ExecutorService.

您可以拥有Map ,并将您在数据库中找到的所有邮件提交给相应的执行程序服务。使用Executors创建ExecutorService的适当实现。 ,executorservice>

#1


0  

That's basically the idea of an ExecutorService. You create an executor service backed by one or several threads (a thread pool), and you submit tasks to this executor service. The tasks are stored in a queue, from which the thread(s) of the executor service takes the tasks it must execute. When the queue is empty, the thread(s) wait for a new task to appear in the queue.

这基本上是ExecutorService的想法。您创建由一个或多个线程(线程池)支持的执行程序服务,并将任务提交给此执行程序服务。任务存储在队列中,执行程序服务的线程从该队列中获取必须执行的任务。当队列为空时,线程等待新任务出现在队列中。

You could have a Map<SmtpServer, ExecutorService>, and submit all the mails you have found in the database to the appropriate executor service. Use Executors to create the appropriate implementation of ExecutorService.

您可以拥有Map ,并将您在数据库中找到的所有邮件提交给相应的执行程序服务。使用Executors创建ExecutorService的适当实现。 ,executorservice>