简体   繁体   English

Java静态同步与BlockingQueue实现

[英]Java Static Synchronized vs BlockingQueue implementation

I'm trying to implement an appointment queue using Servlets (resteasy + Hibernate). 我正在尝试使用Servlet(resteasy + Hibernate)实现约会队列。 my appointment controller is as follows (simplified of course). 我的约会控制器如下(当然是简化的)。

public class AppoController{

public synchronized static int createAppoinment(AppObj app){
   //get last app no
   //insert new app with no+1
   //return new app no
}
}

currently this method works fine. 目前,此方法效果很好。 but i've read about BlockingQueue implementations which seems the correct way? 但我读过有关BlockingQueue实现的方法,这似乎是正确的方法?

Definition of working Fine: 工作罚款的定义:
if i dont use synchronized static and send multiple requests at once multiple appointments have same appointment no 如果我不使用同步静态并且一次发送多个请求,则多个约会有相同的约会
but if i use synchronized static, appointments created with correct order 但是,如果我使用同步静态,则按正确的顺序创建约会

i dont use any threads here but i assume tomcat uses its own threads to server http requests from users. 我在这里不使用任何线程,但我假设tomcat使用其自己的线程来处理来自用户的http请求。 so this is a multithreaded app? 所以这是一个多线程的应用程序?

i have googled it for past few days but the closest i got is Java/Android: Synchronized vs Queue implementation 我已经用谷歌搜索了几天,但最接近的是Java / Android:同步与队列实现

what i need to clarify are; 我需要澄清的是;
- is this the correct way to do this? -这是正确的方法吗?
- what are the pros and cons of using synchronized static vs BlockingQueue implementation for my scenario. -在我的方案中使用同步静态与BlockingQueue实现的优缺点是什么?

Any other inputs you seem relevant are also welcome. 也欢迎您认为相关的其他任何输入。 Thanks. 谢谢。

Your implementation does work. 您的实现确实起作用。 The synchronized method can only be executed by one thread at any time. 同步方法只能在任何时间由一个线程执行。 Tomcat will use multiple threads (the details depend on the current settings, lets assume one thread per request), so every concurrent request will get its own thread and then ever request waits on this methods until its thread is allowed to enter the method. Tomcat将使用多个线程(详细信息取决于当前设置,假设每个请求一个线程),因此每个并发请求将获得其自己的线程,然后任何请求都将等待该方法,直到允许其线程进入该方法为止。

I see two options depending on your needs. 根据您的需求,我看到两个选择。

  1. If the appointment comes from a database, lets the database or hibernate handle the id generation. 如果约会来自数据库,则让数据库或休眠处理ID生成。 That would move the multi thread problem to the database which was designed to handle that kind of problem. 这会将多线程问题移到旨在处理此类问题的数据库中。
  2. If the appointment does not come from a database and you just need a unique identifier for the appointment objects use a UUID, eg java.lang.UUID.randomUUID() 如果约会不是来自数据库,而您只需要约会对象的唯一标识符,则使用UUID,例如java.lang.UUID.randomUUID()

Actually using a Queue will only make sense if you want move the creation of the appointment out of the http request. 实际上,仅当您要将约会的创建从http请求中移出时,才使用Queue才有意义。 Eg if you create the appointment after the request finished like a nightly batch job or a dedicated worker thread pool. 例如,如果您在请求完成后创建约会,例如每晚批处理作业或专用工作线程池。 But this will only make sense if it is a expensive operation to create an appointment. 但这仅在创建约会的操作昂贵时才有意义。

On a different topic, you should check if this method needs to be static. 在另一个主题上,您应该检查此方法是否需要静态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM