`
Digger
  • 浏览: 33975 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
AVI
社区版块
存档分类
最新评论

Problems with threads --- Simple Observer Pattern

UP 
阅读更多
We usually use observer pattern like following


[i]public synchronized  void addListener(listener) {…}

public synchronized  void setValue(newValue) {
    myValue = newValue;

    for (int i = 0; i < myListeners.length; i++) {
        myListeners[i].valueChanged(newValue)
    }
}[/i]




What’s wrong with this (in a multithreaded context)?
分享到:
评论
4 楼 Digger 2008-10-22  
DEFINITION - A deadlock is a situation in which two computer programs sharing the same resource are effectively preventing each other from accessing the resource, resulting in both programs ceasing to function.

The earliest computer operating systems ran only one program at a time. All of the resources of the system were available to this one program. Later, operating systems ran multiple programs at once, interleaving them. Programs were required to specify in advance what resources they needed so that they could avoid conflicts with other programs running at the same time. Eventually some operating systems offered dynamic allocation of resources. Programs could request further allocations of resources after they had begun running. This led to the problem of the deadlock. Here is the simplest example:

  Program 1 requests resource A and receives it.
  Program 2 requests resource B and receives it.
  Program 1 requests resource B and is queued up, pending the release of B.
  Program 2 requests resource A and is queued up, pending the release of A.

Now neither program can proceed until the other program releases a resource. The operating system cannot know what action to take. At this point the only alternative is to abort (stop) one of the programs.

Learning to deal with deadlocks had a major impact on the development of operating systems and the structure of databases. Data was structured and the order of requests was constrained in order to avoid creating deadlocks.
3 楼 Digger 2008-10-22  
看到一篇文章,专门针对这个问题,提出这样写法存在安全问题。(可惜找不到了)

我也在思考,问题到底在哪儿?

DEADLOCK ?
2 楼 Digger 2008-10-15  
No. 

1 楼 vvgg 2008-10-15  
没有问题是线程安全的

相关推荐

    jboss-threads-3.1.0.Final-API文档-中文版.zip

    赠送jar包:jboss-threads-3.1.0.Final.jar; 赠送原API文档:jboss-threads-3.1.0.Final-javadoc.jar; 赠送源代码:jboss-threads-3.1.0.Final-sources.jar; 赠送Maven依赖信息文件:jboss-threads-3.1.0.Final....

    Node.js-worker-threads-pool轻松管理Node.js工作线程池

    "worker-threads-pool" 是一个专门为Node.js设计的库,它的目的是简化工作线程池的管理和操作。工作线程池可以有效地将CPU密集型任务分配到不同的工作线程中,从而提高程序的执行效率。这个库的主要功能包括创建...

    jboss-threads-3.1.0.Final-API文档-中英对照版.zip

    赠送jar包:jboss-threads-3.1.0.Final.jar; 赠送原API文档:jboss-threads-3.1.0.Final-javadoc.jar; 赠送源代码:jboss-threads-3.1.0.Final-sources.jar; 赠送Maven依赖信息文件:jboss-threads-3.1.0.Final....

    mingw-std-threads-master

    mingw-std-threads-master介绍 mingw-std-threads-master是一个为MinGW(Minimalist GNU for Windows)环境提供的标准线程库实现。MinGW是一个开源项目,它提供了在Windows平台上使用GNU工具集(如GCC)进行软件...

    perl-threads-shared-1.43-6.el7.x86_64.rpm

    离线安装包,亲测可用

    perl-threads-shared-1.58-2.el8.ppc64le.rpm

    离线安装包,亲测可用

    Programming with POSIX Threads - Part 1

    Programming with POSIX Threads by David Butenhof;Programming with POSIX Threads by David Butenhof;Programming with POSIX Threads by David Butenhof;

    Python库 | signalr-client-threads-0.0.12.tar.gz

    Python库`signalr-client-threads-0.0.12.tar.gz`是一个专门为Python开发者设计的 SignalR 客户端库,它允许你在Python应用程序中与使用SignalR服务的Web应用进行实时通信。SignalR是ASP.NET框架下的一项技术,用于...

    Programming with POSIX Threads - Part 3

    Programming with POSIX Threads by David ButenhofProgramming with POSIX Threads by David ButenhofProgramming with POSIX Threads by David ButenhofProgramming with POSIX Threads by David Butenhof

    Programming with POSIX Threads - Part 4

    Programming with POSIX Threads by David ButenhofProgramming with POSIX Threads by David ButenhofProgramming with POSIX Threads by David ButenhofProgramming with POSIX Threads by David Butenhof

    Programming with POSIX Threads - Part 2

    Programming with POSIX Threads by David ButenhofProgramming with POSIX Threads by David ButenhofProgramming with POSIX Threads by David ButenhofProgramming with POSIX Threads by David Butenhof

    java-threads-tutorial.zip_between

    get started writing simple programs that use them. We will also explore the basic building blocks of more sophisticated threading applications -- how to exchange data between threads, how to control ...

    linux实用脚本show-busy-java-threads

    本文将深入探讨标题所提及的三个实用脚本:“show-busy-java-threads”、“show-duplicate-java-classes”以及“find-in-jars”。这些脚本都是针对Java开发者和系统管理员的利器,旨在提高效率和解决问题。 1. **...

    test-threads-init.rar_threads

    "test-threads-init.rar_threads"这个标题暗示了这是一个关于测试线程延迟初始化的项目。下面我们将深入探讨线程初始化、延迟初始化及其相关的编程实践。 1. **线程的基本概念**: - **进程与线程**:进程是操作...

    Python库 | Flask_Threads-0.0.1a0-py3.6.egg

    python库。 资源全名:Flask_Threads-0.0.1a0-py3.6.egg

    Python库 | hanging_threads-1.1.0-py2-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:hanging_threads-1.1.0-py2-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    PyPI 官网下载 | requests_threads-0.1.0.tar.gz

    `requests_threads-0.1.0.tar.gz` 是一个源自PyPI(Python Package Index)官网的压缩包,其中包含了一个名为 `requests_threads` 的Python库的源代码。这个库是针对Python的 `requests` 库的一个扩展,它允许开发者...

Global site tag (gtag.js) - Google Analytics