java Java中同步方法的缺点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1365880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:16:47  来源:igfitidea点击:

Disadvantage of synchronized methods in Java

javasynchronization

提问by Madhu

What are the disadvantages of making a large Java non-static method synchronized? Large method in the sense it will take 1 to 2 mins to complete the execution.

使大型 Java 非静态方法同步的缺点是什么?从某种意义上说,大型方法需要 1 到 2 分钟才能完成执行。

回答by Thilo

If you synchronize the method and try to call it twice at the same time, one thread will have to wait two minutes.

如果同步该方法并尝试同时调用它两次,则一个线程将不得不等待两分钟。

This is not really a question of "disadvantages". Synchronization is either necessary or not, depending on what the method does.

这不是真正的“缺点”问题。同步是必要的还是不必要的,这取决于方法的作用。

If it is critical that the code runs only once at the same time, then you need synchronization.

如果代码在同一时间只运行一次很重要,那么您需要同步。

If you want to run the code only once at the same time to preserve system resources, you may want to consider a counting Semaphore, which gives more flexibility (such as being able to configure the number of concurrent executions).

如果您想同时只运行一次代码以保留系统资源,您可能需要考虑计数Semaphore,它提供了更大的灵活性(例如能够配置并发执行的数量)。

Another interesting aspect is that synchronization can only really be used to control access to resources within the same JVM. If you have more than one JVM and need to synchronize access to a shared file system or database, the synchronized keyword is not at all sufficient. You will need to get an external (global) lock for that.

另一个有趣的方面是同步只能真正用于控制对同一 JVM 中资源的访问。如果您有多个 JVM 并且需要同步对共享文件系统或数据库的访问,则 synchronized 关键字根本不够用。您需要为此获得一个外部(全局)锁。

回答by user57368

If the method takes on the order of minutes to execute, then it may not need to be synchronized at such a coarse level, and it may be possible to use a more fine-grained system, perhaps by locking only the portion of a data structure that the method is operating on at the moment. Certainly, you should try to make sure that your critical section isn't really 2 minutes long - any method that takes that long to execute (regardless of the presence of other threads or locks) should be carefully studied as a candidate for parallelization. For a computation this time-consuming, you could be acquiring and releasing hundreds of locks and still have it be negligible. (Or, to put it another way, even if you need to introduce a lot of locks to parallelize this code, the overhead probably won't be significant.)

如果该方法需要几分钟的时间来执行,那么它可能不需要在如此粗略的级别上同步,并且可能使用更细粒度的系统,也许通过只锁定数据结构的一部分该方法目前正在运行。当然,您应该尝试确保您的临界区不是真正的 2 分钟长 - 任何需要那么长时间执行的方法(无论是否存在其他线程或锁)都应该作为并行化的候选者进行仔细研究。对于如此耗时的计算,您可能会获取和释放数百个锁,但仍然可以忽略不计。(或者,换句话说,即使您需要引入大量锁来并行化此代码,开销也可能不会很大。)

回答by Peter Recore

Since your method takes a huge amount of time to run, the relatively tiny amount of time it takes to acquire the synchronized lock should not be important.

由于您的方法需要大量时间来运行,因此获取同步锁所花费的相对较少的时间应该不重要。

A bigger problem could appear if your program is multithreaded (which I'm assuming it is, since you're making the method synchronized), and more than one thread needs to access that method, it could become a bottleneck. To prevent this, you might be able to rewrite the method so that it does not require synchronization, or use a synchronized block to reduce the size of the protected code (in general, the smaller the amount of code that is protected by the synchronize keyword, the better).

如果您的程序是多线程的(我假设它是多线程的,因为您使方法同步),并且多个线程需要访问该方法,则可能会出现更大的问题,这可能会成为瓶颈。为了防止这种情况,您可以重写该方法使其不需要同步,或者使用同步块来减少受保护代码的大小(通常,受同步关键字保护的代码量越小, 更好)。

You can also look at the java.util.concurrent classes, as you may find a better solution there as well.

您还可以查看 java.util.concurrent classes,因为您也可以在那里找到更好的解决方案。

回答by Stephen C

If the object is shared by multiple threads, if one thread tries to call the synchronized method on the object while another's call is in progress, it will be blocked for 1 to 2 minutes. In the worst case, you could end up with a bottleneck where the throughput of your system is dominated by executing these computations one at a time.

如果对象被多个线程共享,如果一个线程尝试调用对象上的同步方法,而另一个线程正在调用,则会被阻塞 1 到 2 分钟。在最坏的情况下,您最终可能会遇到瓶颈,即一次执行这些计算来控制系统的吞吐量。

Whether this is a problem or not depends on the details of your application, but you probably should look at more fine-grained synchronization ... if that is practical.

这是否是一个问题取决于您的应用程序的详细信息,但您可能应该查看更细粒度的同步……如果可行的话。

回答by Stephen C

In simple two lines Disadvantage of synchronized methods in Java :

在简单的两行 Java 中同步方法的缺点:

  1. Increase the waiting time of the thread
  2. Create performance problem
  1. 增加线程的等待时间
  2. 产生性能问题

回答by Yash Patel

First drawback is that threads that are blocked waiting to execute synchronize code can't be interrupted.Once they're blocked their stuck there, until they get the lock for the object the code is synchronizing on.

第一个缺点是被阻塞等待执行同步代码的线程不能被中断。一旦它们被阻塞,它们就会卡在那里,直到它们获得代码同步对象的锁。

Second drawback is that the synchronized block must be within the same method in other words we can't start a synchronized block in one method and end the syncronized block in another for obvious reasons.

第二个缺点是同步块必须在同一个方法中,换句话说,由于显而易见的原因,我们不能在一种方法中启动同步块并在另一种方法中结束同步块。

The third drawback is that we can't test to see if an object's intrinsic lock is available or find out any other information about the lock also if the lock isn't available we can't timeout after we waited lock for a while. When we reach the beginning of a synchronized block we can either get the lock and continue executing or block at that line of code until we get the lock.

第三个缺点是我们不能测试一个对象的内在锁是否可用,或者找出关于锁的任何其他信息,如果锁不可用,我们不能在等待锁一段时间后超时。当我们到达同步块的开头时,我们可以获取锁并继续执行,也可以在该行代码处阻塞,直到获得锁。

The fourth drawback is that if multiple threads are awaiting to get lock, it's not first come first served. There isn't set order in which the JVM will choose the next thread that gets the lock, so the first thread that blocked could be the last thread to get the lock and vice Versa.

第四个缺点是,如果多个线程正在等待获得锁定,则不是先到先得。JVM 没有设置顺序来选择下一个获得锁的线程,所以第一个被阻塞的线程可能是最后一个获得锁的线程,反之亦然。

so instead of using synchronization we can prevent thread interference using classes that implement the java.util.concurrent locks.lock interface.

因此,我们可以使用实现 java.util.concurrent locks.lock 接口的类来防止线程干扰,而不是使用同步。

回答by Swapnil G Thaware

In simple two lines Disadvantage of synchronized methods in Java :

在简单的两行 Java 中同步方法的缺点:

 1. Increase the waiting time of the thread
 2. Create a performance problem