同步静态方法在Java中如何工作? [英] How do synchronized static methods work in Java?

查看:90
本文介绍了同步静态方法在Java中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个具有静态方法的util类,它将调用Hibernate函数来完成基本的数据访问。我想知道是否让方法 synchronized 是确保线程安全的正确方法。



我想这样做阻止信息访问相同的数据库实例。不过,我现在确定下面的代码是否阻止 getObjectById 被所有类调用,当它被特定的类调用时。

  public class Utils {
public static synchronized Object getObjectById(Class objclass,Long id){
/ / call hibernate class
Session session = new Configuration()。configure()。buildSessionFactory()。openSession();
Object obj = session.load(objclass,id);
session.close();
return obj;
}

//其他静态方法
}


解决方案

通过使用静态方法锁定的同步,您将同步类方法和属性(而不是实例方法和属性)

所以你的假设是正确的。


我想知道是否让方法同步是确保线程安全的正确方法。


不是。你应该让这个工作做你的RDBMS。他们擅长这种东西。

通过同步数据库的访问权限,唯一的办法就是让应用程序非常慢。此外,在您发布的代码中,您每次都会构建一个Session Factory,这样,您的应用程序将花费更多时间访问数据库,而不是执行实际工作。假设以下情况:

客户端A和B尝试向表T的记录X中插入不同的信息 。

使用你的方法,你得到的唯一东西就是确保一个在另一个之后被调用,而这在数据库中会发生,因为RDBMS会防止他们同时从A插入一半信息和从B插入一半信息。结果将是相同的,但速度只有5倍(或更多)。

可能最好看一看 Hibernate文档中的Transactions and Concurrency章节。大多数情况下,您尝试解决的问题已经得到解决,并且是一种更好的方法。

If I have a util class with static methods that will call Hibernate functions to accomplish basic data access. I am wondering if making the method synchronized is the right approach to ensure thread-safety.

I want this to prevent access of info to the same DB instance. However, I'm now sure if the following code are preventing getObjectById being called for all Classes when it is called by a particular class.

public class Utils {
     public static synchronized Object getObjectById (Class objclass, Long id) {
           // call hibernate class
         Session session = new Configuration().configure().buildSessionFactory().openSession();
         Object obj = session.load(objclass, id);
         session.close();
         return obj;
     }

     // other static methods
}

解决方案

By using synchronized on a static method lock you will synchronize the class methods and attributes ( as opposed to instance methods and attributes )

So your assumption is correct.

I am wondering if making the method synchronized is the right approach to ensure thread-safety.

Not really. You should let that work do your RDBMS instead. They are good at this kind of stuff.

The only thing you will get by synchronizing the access to the database is to make your application terribly slow. Further more, in the code you posted you're building a Session Factory each time, that way, your application will spend more time accessing the DB than performing the actual job.

Imagine the following scenario:

Client A and B attempt to insert different information into record X of table T.

With your approach the only thing you're getting is to make sure one is called after the other, when this would happen anyway in the DB, because the RDBMS will prevent them from inserting half information from A and half from B at the same time. The result will be the same but only 5 times ( or more ) slower.

Probably it could be better to take a look at the "Transactions and Concurrency" chapter in the Hibernate documentation. Most of the times the problems you're trying to solve, have been solved already and a much better way.

这篇关于同步静态方法在Java中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆