有不同级别的同步吗?

发布于 2024-08-18 03:14:16 字数 42 浏览 4 评论 0原文

我听说同步有不同级别。有吗?(如果有,请用片段代码解释一下吗?)谢谢。

I have heard that there are different levels of Synchronization.are there?(If there are ,would you please explain them with a snippet code?)thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

肥爪爪 2024-08-25 03:14:16

在 Java 5 之前,只有一个:synchronized 关键字。这等待并获得了引用对象的独占锁。当应用于函数时:

public synchronized void doStuff() { ... }

同步的对象是 this

Java 5 添加了很多并发实用程序,其中之一是 锁定对象。有多个版本,包括 读写锁。这是我能想到的唯一你可能提到的事情。

synchronized 的问题在于它相当粗糙。做得不好可能会导致僵局。 Java 5 utils 允许非阻塞锁获取、锁获取超时和读/写锁支持。

Before Java 5 there was only one: the synchronized keyword. This waited for and obtained an exclusive lock on the reference object. When applied to a function:

public synchronized void doStuff() { ... }

the object being synchronized on is this.

Java 5 added a lot of concurrency utils, one of which was the Lock object. There are several versions of this including a ReadWriteLock. This is the only thing I can think of that you might be referring to.

The problem with synchronized is that its fairly crude. Done badly it can lead to deadlocks. The Java 5 utils allow non-blocking lock acquisition, timeout on lock acquisition and read/write lock support.

再可℃爱ぅ一点好了 2024-08-25 03:14:16

您确实需要解释“同步级别”的含义。 之间的区别吗

public synchronized void foo()
{
    ...
}

您是在谈论:和

public void foo()
{
    synchronized(lock)
    {
        ...
    }
}

?或者也许在上述内容和使用 java.util.concurrent.locks?

如果您能提供更多您所听到的背景信息,我们也许能够为您提供更好的帮助。更重要的是,您认为可能需要此信息来尝试解决什么问题?

You'd really need to explain what you meant by a "level of synchronization". Are you talking about the difference between:

public synchronized void foo()
{
    ...
}

and

public void foo()
{
    synchronized(lock)
    {
        ...
    }
}

? Or perhaps between the above and using the locks from java.util.concurrent.locks?

If you could give more context to what you've heard, we may be able to help you better. More importantly, what problem are you trying to solve that you think might need this information?

め七分饶幸 2024-08-25 03:14:16

我假设 OP 指的是多线程中使用的对象同步,而不是 Java 中的关键字。

这是我在阅读 Goetz 后对不同级别的同步的理解爪哇。

假设我们有一个必须在 132 个线程中使用的对象,这些线程连续、完全随机地使用该对象,比如说 100 年。

abstract Class ICount {
int i;
String iString;
public void add(int i);
public void sub(int i);
public void synchronized print(){
  assert(i == Integer.valueOf(iString));
  System.out.println("i"+String.valueof(i)+" == "+iString);
   }
}

现在可能有不同的实现,无论是出于设计还是由于粗心,在不同级别上都是线程安全的。

  1. 线程安全同步实现:保证任何不使用反射来破坏对象的java代码,使用此实现,永远不会命中断言。
  2. 并发同步实现:除了保证线程安全之外,该实现的作者还尝试在可能的情况下允许并发执行,并尝试避免锁(争用)。
  3. 线程不安全同步实现:这一点的开发人员至少知道他的类不是线程安全的,并要求您不要使用此实现,除非您只有一个线程。

如果这是一个真正的问题,则大多数实际实现属于“同步”的第四类,即它有时有效,并且没有足够的文档来分类为上述类别之一,因此默认属于第四类。

I am assuming OP is referring to synchronization of object used in multiple thread and not the keyword in Java.

This is my understanding, after reading Goetz, of different levels of synchronizations in Java.

Lets assume we have a object that we have to use across 132 threads, which use the object continuously, purely randomly, over say 100 years.

abstract Class ICount {
int i;
String iString;
public void add(int i);
public void sub(int i);
public void synchronized print(){
  assert(i == Integer.valueOf(iString));
  System.out.println("i"+String.valueof(i)+" == "+iString);
   }
}

Now there could be different implementation which either by design or by carelessness, be thread safe at different levels.

  1. Thread safe synchronization implementation : Guarantees that any java code, that doesn't use reflection to screw the object, using this implementation, will never hit assert.
  2. Concurrent synchronization implementation : In addition to guaranteeing thread-safety, the author of this implementation tried to allow concurrent executions when possible, and also gave a shot at avoiding locks (contentions).
  3. Thread unsafe synchronization implementation : Developer of this, at least knows his class his not thread safe, and requests you not to use this implementation unless you have exactly one thread.

If this were a real problem, the majority of the practical implementation falls in fourth category of 'synchronization', which is it works sometimes, and not documentation sufficiently to categorize in one of the above categories, and hence by default falls in fourth category.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文