Singelton模式与多线程
我阅读了具有多线程的Sigelton模式,发现已实现的使用同步。
我的问题是我可以使用wait() + notify()或notifyall()而不是同步? 如果是,哪个更好地同步或witch()+ notifyall()???
I read about sigelton pattern with multithreads and I found that is implemented use synchronized .
my question is can I use wait() + notify() or notifyAll() instead synchronized ??
and if yes which better synchronized or wait()+ notifyAll() ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
方法等待和通知仅在同步方法/块中有意义。
尝试以下操作:
这将导致:
这是因为当前线程不拥有监视器。
现在尝试一下,您正在经营(尽管还不是一个非常有用的业务):
该线程现在无限期地等待永远不会出现的通知。
现在尝试以下操作:
将A线程在监视器上同步并等待通知。
线B入睡三秒钟,然后在显示器上同步并通知。
示例输出:
数字是我记录的线程ID,以清晰。
现在,您对单身人士的疑问。有几种方法可以执行一个类的实例。最常见的是,像Spring这样的框架在应用程序上下文中强制执行其Bean的单个实例。这是一种接受务实的方法。为了在类加载程序中执行单例,最好的方法是使用枚举,因为它避免了序列化问题,但通常是使用私有构造函数和静态实例字段来完成的。请注意,这种自我管理的单例被大多数人视为反塔。
单身人士通常仅在类加载程序中唯一。如果您在应用程序服务器上运行,可能会有多个类加载程序,因此您必须小心要在此上下文中要使用此Singleton的上下文。在这种情况下,最好避免单身人士,而是创建一个单独的可部署服务或存储库来保留数据。
如果您确实有单身人士并希望能够以线程安全的方式访问它,您将必须同步所有访问它。您不一定需要等待/通知此。例如:
示例输出:
仅通过使用同步,就可以将访问权限成为独家。只有当线程需要另一个线程的结果时,才能等待/通知才能发挥作用。
The methods wait and notify are only meaningful within a synchronized method/block.
Try this:
It will result in:
This is because the current thread does not own the monitor.
Now try this, and you're in business (although not a very useful business yet):
The thread now waits indefinitely for the notify that will never come.
Now try this:
Thread a synchronizes on the monitor and waits for a notification.
Thread b sleeps for three seconds, then synchronizes on the monitor and notifies.
Example output:
The numbers are the thread ID's which I logged for clarity.
Now to your question about a singleton. There are several ways to enforce that there is only one instance of a class. Most commonly, a framework like Spring enforces single instances of its beans within the application context. This is an accept pragmatic approach. To enforce a singleton within a class loader, probably the best way is to use an enum because it avoids serialization issues, but it's often done with a private constructor and a static instance field. Note that this type of self-managed singletons is considered an anti-pattern by most.
A singleton is normally only unique within a class loader. If you are running on an application server there might be multiple class loaders so you have to be careful there about the context in which you want to be able to use this singleton. In such a situation it might be better to avoid the singleton and instead create a separate deployable service or repository to keep the data in.
If you do have a singleton and want to be able to access it in a thread-safe manner, you will have to synchronize all access to it. You don't necessarily need wait/notify for this. For example:
Example output:
Just by using synchronized already the access can be made exclusive. Only when a thread needs the results of another thread does wait/notify come into play.
答案都是相同的,无论您是要实施单例还是实施其他任何东西。
否
同步
是Java程序中的线程可以安全共享变量的机制。它提供了两件事:它在访问共享变量时会阻止线程从中互相干扰。
它可以确保以可预测且及时的方式对其他线程的共享变量进行更新。
wait()
,notify()
和notifyall()
是一种机制,一个线程可以通知其他线程已更改共享变量以某种特殊的方式。wait()
或notify()
或 notifyall()不要技术需要使用同步
,但是Java语言设计师任意地添加了要求,以提醒您,如果您要通知其他有关共享变量的事物,或者即使您只能去 在另一个线程对它们所做的事情上,您将需要同步
才能安全访问变量。The answer is the same regardless of whether you are trying to implement a singleton or implement anything else.
No.
synchronized
is a mechanism by which threads in a Java program can safely share variables. It provides two things:It prevents threads from interfering with each other when they access the shared variables.
It ensures that updates to shared variables made by one thread will become visible to other threads in a predictable and timely way.
wait()
,notify()
, andnotifyAll()
are a mechanism by which one thread can notify other threads that it has changed shared variables in some particular way.wait()
ornotify()
ornotifyAll()
don't technically require the use ofsynchronized
, but the Java language designers arbitrarily added that requirement as a reminder that, if you're going to notify another thread about something you did to shared variables, or even if you're only going to look at something that another thread did to them, you're going to needsynchronized
to safely access the variables.