简体   繁体   English

单线程的Java同步方法

[英]Java synchronized methods for a single thread

I'm having trouble understanding the synchronized keyword. 我在理解synced关键字时遇到了麻烦。 As far as I know, it is used to make sure that only one thread can access the synchronized method/block at the same time. 据我所知,它用于确保只有一个线程可以同时访问同步的方法/块。 Then, is there sometimes a reason to make some methods synchronized if only one thread calls them? 然后,如果只有一个线程调用某些方法,有时是否有理由使某些方法同步?

If your program is single threaded, there's no need to synchronize methods. 如果您的程序是单线程的,则无需同步方法。

Another case would be that you write a library and indicate that it's not thread safe. 另一种情况是您编写了一个库,并指出它不是线程安全的。 The user would then be responsible for handling possible multi-threading use, but you could write it all without synchronization. 然后,用户将负责处理可能的多线程使用,但是您可以全部编写它们而无需同步。

If you are sure your class will be always used under single thread there is no reason to use any synchronized methods. 如果您确定您的类将始终在单线程下使用,则没有理由使用任何同步方法。 But, the reality is - Java is inherently multi threaded environment. 但是,现实是-Java本质上是多线程环境。 At some point of time somebody will use multiple threads. 在某个时间点,有人会使用多个线程。 Therefore whichever class needs thread safety should have adequately synchonized methods/synchronized blocks to avoid problems. 因此,无论哪个类需要线程安全,都应具有足够的同步方法/同步块,以避免出现问题。

No , you don't need synchronization if there is single thread involved. ,如果涉及单个线程,则不需要同步。

Always specify thread-safety policy 始终指定线程安全策略

Actually you never know how a class written by you is going to be used by others in future . 实际上, 您永远都不知道将来您的人将如何使用您编写的课程 So it is always better to explicitly state your policy. 因此,最好明确声明您的政策。 So that if in future someone tries to use it in multi-threaded way then they can be aware of the implications. 这样,如果将来有人尝试以多线程方式使用它,那么他们可以意识到其中的含义。

And the best place to specify the thread-safety policy is in JavaDocs. 而且,指定线程安全策略的最佳位置是JavaDocs。 Always specify in JavaDocs as to whether the class that you are creating is thread safe or not. 始终在JavaDocs中指定要创建的类是否是线程安全的。

When two or more threads need access to a shared resource , they need some way to ensure that the resource will be used by only one thread at a time. 当两个或多个线程需要访问shared resource ,它们需要某种方式来确保一次只能由一个线程使用该资源。

Synchronized method is used to block the Shared resource between the multiple Threads. 同步方法用于阻止多个线程之间的共享资源。

So, No need to apply Synchronization for the Single Thread 因此,无需对单线程应用同步

Consider that you are designing a movie ticket seller application. 考虑到您正在设计电影票销售者应用程序。 And lets drop all the technology capabilities that are provided these days, for the sake of visualizing the problem. 为了可视化问题,让我们放弃这些天提供的所有技术功能。

There is only one ticket left for the show with 5 different counters selling tickets. 演出只剩下一张票,还有5个不同的柜台出售票。 Consider there are 2 people trying to buy the last ticket of the show at the counters. 考虑到有两个人试图在柜台购买演出的最后一张票。 Consider your application workflow to be such 考虑您的应用程序工作流程是这样的

  1. You take in details of the buyer, his name, and his credit card 您需要详细说明买家,他的名字和他的信用卡
    number. 数。 (this is the read operation) (这是读取操作)
  2. Then you find out how many tickets are left for the show (this is again a read operation) 然后,您可以找到剩余的演出门票(这也是一次读取操作)
  3. Then you book the ticket with the credit card (this is the write operation) 然后用信用卡预订机票(这是写操作)

If this logic isnt synchronised, what would happen? 如果这种逻辑不同步,将会发生什么?

The details of Customer 1 and Customer 2 would be read up until step 2. Both will try to book the ticket and both their tickets would be booked. 客户1和客户2的详细信息将一直读取到第2步。双方都将尝试预订机票,并且他们的机票都将被预订。

If it is modified to be 如果修改为

  1. You take in details of the buyer, his name, and his credit card number. 您需要详细说明买方,他的名字和他的信用卡号。 (this is the read operation) (这是读取操作)

Synchronize ( Synchronize

  1. Then you find out how many tickets are left for the show (this is again a read operation) 然后,您可以找到剩余的演出门票(这也是一次读取操作)
  2. Then you book the ticket with the credit card (this is the write operation) ) 然后您用信用卡预订票(这是写操作))

There is no chance of overbooking the show due to a thread race condition. 由于线程竞争的情况,没有机会超额预定演出。

Now, consider this example where you absolutely know that there will be only and only 1 person booking tickets. 现在,考虑这个示例,您绝对知道只有1人预订票。 There is no need for synchronization. 无需同步。

The ticket seller here would be your single thread in case of your application 如果您有申请,这里的售票员将是您的单线程

I have tried to put this in a very very simplistic manner. 我试图以非常非常简单的方式来说明这一点。 There are frameworks, and constraints which you put on the DB to avoid such a simple scenario. 为了避免这种简单的情况,您在数据库上放置了一些框架和约束。 But the intent of the answer is to prove the theory of why thread synchronization, and not the capabilities of the way to avoid it. 但是答案的目的是证明为什么线程同步的理论,而不是避免同步的能力。

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

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