`
lvhuiqing
  • 浏览: 251568 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

1.4. Threads are Everywhere(线程无处不在)

阅读更多
1.4. Threads are Everywhere(线程无处不在)
Even if your program never explicitly creates a thread, frameworks may create threads on your behalf, and code called from these threads must be thread-safe. This can place a significant design and implementation burden on developers, since developing thread-safe classes requires more care and analysis than developing non-thread-safe classes.
即使你的程序没有显式的创建线程,但是你所用的框架可能会为你创建线程,这就要求被这些线程调用的代码必须是线程安全的。这就要求开发者在设计和实现的时候要充分考虑和分析。
Every Java application uses threads. When the JVM starts, it creates threads for JVM housekeeping tasks (garbage collection, finalization) and a main thread for running the main method. The AWT (Abstract Window Toolkit) and Swing user interface frameworks create threads for managing user interface events. Timer creates threads for executing deferred tasks. Component frameworks, such as servlets and RMI create pools of threads and invoke component methods in these threads.
每一个java应用程序都会用到线程。当虚拟机开始启动的时候,虚拟机会为JVM启动一些内部事务管理的线程(垃圾回收、资源释放)还有一个main线程来执行main函数。AWT和Swing这样的用户界面框架会创建响应用户时间的线程。计时器会穿件执行延期任务的线程。组件框架,例如servlet和RMI,会创建线程池,并且会在这些线程中机会组件方法。
If you use these facilities as many developers do you have to be familiar with concurrency and thread safety, because these frameworks create threads and call your components from them. It would be nice to believe that concurrency is an "optional" or "advanced" language feature, but the reality is that nearly all Java applications are multithreaded and these frameworks do not insulate you from the need to properly coordinate access to application state.
如果像其他大多数程序员一样,想要使用这样的机制的话,你必须要熟悉并发机制和线程安全,因为这样的框架会创建线程,并从这些线程中调用你开发的组件。如果你依然认为并发是一种“可选的”和“高级的”语言特性,这或许不是完全错误的,但事实上几乎所有的java应用程序都是多线程的,而且所有的框架可能会要求你去恰当的处理应用的状态。
When concurrency is introduced into an application by a framework, it is usually impossible to restrict the concurrency-awareness to the framework code, because frameworks by their nature make callbacks to application components that in turn access application state. Similarly, the need for thread safety does not end with the components called by the framework it extends to all code paths that access the program state accessed by those components. Thus, the need for thread safety is contagious.
当并发被某个框架引入到应用系统中的时候,通常,如果要求框架本身具有并发控制本身是不太可能的。因为框架本身是通过回调应用程序的组件来访问程序的状态。同样的,线程安全不可能在被框架直接调用的组件中得到解决,它势必会扩展到那些访问程序状态的组件中。因此,线程安全的问题级联的。
Frameworks introduce concurrency into applications by calling application components from framework threads. Components invariably access application state, thus requiring that all code paths accessing that state be thread-safe.
框架通过在框架线程中调用应用组件来引入并发。而组件一定是用来访问程序状态的,这就要求所有访问程序状态的代码层次都必须是线程安全的。

The facilities described below all cause application code to be called from threads not managed by the application. While the need for thread safety may start with these facilities, it rarely ends there; instead, it ripples through the application.
下面描述的所有机制都会通过自身的线程(不是被应用程序所管理的)调用应用代码,当这些机制本身要求线程安全的时候,这就会要求这些应用代码本身也必须是线程安全的。
Timer. Timer is a convenience mechanism for scheduling tasks to run at a later time, either once or periodically. The introduction of a Timer can complicate an otherwise sequential program, because Timer Tasks are executed in a thread managed by the Timer, not the application. If a Timer Task accesses data that is also accessed by other application threads, then not only must the Timer Task do so in a thread-safe manner, but so must any other classes that access that data. Often the easiest way to achieve this is to ensure that objects accessed by the Timer Task are themselves thread-safe, thus encapsulating the thread safety within the shared objects.
Timer,Timer是可以非常便利的是一个任务在一段时间之后运行,或者周期式的运行。Timer会给顺序执行的程序带来某种复杂性,因为一个定时任务运行在一个由Timer管理的线程中,而不是应用程序管理的线程中。如果一个定时器需要访问会被其他应用线程访问的数据,这就必须要求定时器任务使用线程安全的访问方式,其他访问该数据的所有类必须使用线程安全的方式。达到这样效果的最简单的方式是首先保证被定时任务访问的类本身是线程安全的,然后把这种线程安全性封装到其他的共享对象中。
Servlets and JavaServer Pages (JSPs). The servlets framework is designed to handle all the infrastructure of deploying a web application and dispatching requests from remote HTTP clients. A request arriving at the server is dispatched, perhaps through a chain of filters, to the appropriate servlet or JSP. Each servlet represents a component of application logic, and in high-volume web sites, multiple clients may require the services of the same servlet at once. The servlets specification requires that a servlet be prepared to be called simultaneously from multiple threads. In other words, servlets need to be thread-safe.
Servlets和JSP。Servlet架构被设计成用来处理部署一个web应用和分发来自远程http客户端请求的所有基础工作。当一个到达server端的请求会被分发到恰当的servlet或者jsp中进行处理(可能会通过一条filter链)。每个servlet会代表着一个应用处理逻辑。在大型的web站点中,多个客户端可能会同时请求执行servlet的service方法。Servlet规范中要求一个servlet应该能够同时被多个线程执行,换句话说,servlet应该是线程安全的。
Even if you could guarantee that a servlet was only called from one thread at a time, you would still have to pay attention to thread safety when building a web application. Servlets often access state information shared with other servlets, such as application-scoped objects (those stored in the ServletContext) or session-scoped objects (those stored in the per-client HttpSession). When a servlet accesses objects shared across servlets or requests, it must coordinate access to these objects properly, since multiple requests could be accessing them simultaneously from separate threads. Servlets and JSPs, as well as servlet filters and objects stored in scoped containers like ServletContext and HttpSession, simply have to be thread-safe.
即使你能够保证一个servlet在同一个时间只能够被一个线程访问,当你建立web站点的时候,你还是应该注意线程安全问题。Servlet通常会访问被其他servlet共享的状态信息,例如Application作用域的对象(保存在servlet的ServletContext中)或者session作用域的对象(保存在每一个客户端的HttpSession中)。当一个servlet访问servlet和request作用域的共享对象的时候,必须要进行恰当的协调操作,这是因为多个请求可能从互相独立的线程中分别同时访问这些对象。Servlet和Jsp以及filter和保存在ServletContenxt和HttpSession中的对象都应该是线程安全的。
Remote Method Invocation. RMI lets you invoke methods on objects running in another JVM. When you call a remote method with RMI, the method arguments are packaged (marshaled) into a byte stream and shipped over the network to the remote JVM, where they are unpacked (unmarshaled) and passed to the remote method.
远程方法调用。远程方法调用可以让你调用在别的jvm中对象的方法。当使用远程方法调用去访问一个远程方法的时候,方法参数被封装成一个二进制流通过网络传送到远程jvm,在二进制流被远程jvm获取后,流会被解包,然后传送给远程方法。
When the RMI code calls your remote object, in what thread does that call happen? You don't know, but it's definitely not in a thread you created your object gets called in a thread managed by RMI. How many threads does RMI create? Could the same remote method on the same remote object be called simultaneously in multiple RMI threads?[4]
[4] Answer: yes, but it's not all that clear from the Javadoc you have to read the RMI spec.
那么,你知道当你的方法被远程调用的时候,这样的调用是在那个线程中发生的吗?你应该不会知道,因为调用不是发生在你创建的线程中,而是发生在被RMI管理的线程中。你知道RMI创建了几个线程吗?同一个远程对象中的相同方法可以被多个RMI线程同时调用吗?答案是肯定的,但这不是在java文档中说明的,如果你要了解这一点儿,你得去看RMI规范。
A remote object must guard against two thread safety hazards: properly coordinating access to state that may be shared with other objects, and properly coordinating access to the state of the remote object itself (since the same object may be called in multiple threads simultaneously). Like servlets, RMI objects should be prepared for multiple simultaneous calls and must provide their own thread safety.
远程对象必须主要以避免以下两项线程安全隐患。以恰当的方式访问与其他对象共享的状态。两外一个就是以恰当的方式访问有可能被自己改变的状态(这是因为相同的对象可能被多个线程同时访问)。这有点儿像servlet,所有的远程类应该准备好被多个线程同时调用,并且每个对象都要提供线程安全性。
Swing and AWT. GUI applications are inherently asynchronous. Users may select a menu item or press a button at any time, and they expect that the application will respond promptly even if it is in the middle of doing something else. Swing and AWT address this problem by creating a separate thread for handling user-initiated events and updating the graphical view presented to the user.
Swing和AWT架构。用户界面的应用程序在本质上都是异步的。用户有可能在任何时候选择一个下拉菜单或者按下一个按钮。这时候用户可能会期望应用程序立刻做出响应,他们不会去关心在这中间应用程序是否正在做其他的事情。Swing和Awt框架会通过创建一个独立线程的方式来应对该问题,独立线程的任务是处理用户触发的事件,并在图形界面上作出相应的响应。
Swing components, such as JTable, are not thread-safe. Instead, Swing programs achieve their thread safety by confining all access to GUI components to the event thread. If an application wants to manipulate the GUI from outside the event thread, it must cause the code that will manipulate the GUI to run in the event thread instead.
Swing组件,例如JTable,并不是线程安全的。Swing程序通过将所有对用户界面的方法限制在event线程来获取线程安全性。也就是说,如果一个应用程序的想要改变用户界面,它必须调用event线程中相应代码来修改。
When the user performs a UI action, an event handler is called in the event thread to perform whatever operation the user requested. If the handler needs to access application state that is also accessed from other threads (such as a document being edited), then the event handler, along with any other code that accesses that state, must do so in a thread-safe manner.
当用户执行任务一个UI动作的时候,事件处理线程中的一个事件句柄将会被调用来处理该请求。如果该句柄需要访问一个已经被其他线程占用的状态(例如文件正在被剪辑),那么时间处理句柄会被会放入处理队列,使用线程安全的方式访问。
0
0
分享到:
评论

相关推荐

    Java.Threads.3rd.Edition.txt

    根据提供的文件信息,我们可以推断出这是一本关于Java线程技术的书籍——《Java Threads 第三版》。下面将从书中的各个章节提取并总结关键知识点。 ### 一、绪论 #### 5.31.3 为什么需要线程? 在现代编程中,...

    Java.Threads.and.the.Concurrency.Utilities.1484216997

    A second appendix explores how threads are used by various standard class library APIs. Specifically, you learn about threads in the contexts of Swing, JavaFX, and Java 8's Streams API. What you’ll...

    Prentice.Hall.UNIX.Systems.Programming.Communication.Concurrency.and.Threads.2nd.Edition

    《Prentice.Hall.UNIX.Systems.Programming.Communication.Concurrency.and.Threads.2nd.Edition》这本书由Kay A. Robbins和Steven Robbins合著,是关于UNIX系统编程的经典之作,尤其关注通信、并发和线程方面的内容...

    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....

    Programming with POSIX Threads(多线程编程PDF版本)

    在Unix系统和许多类Unix系统中,POSIX线程(POSIX Threads),通常称为Pthreads,是一种使用线程的编程接口,它允许程序设计者在C和C++这样的编程语言中实现多线程。使用POSIX线程库进行多线程编程,可以让开发者...

    Java.Threads.3rd.Edition(chm)

    This book is intended for programmers of all levels who need to learn to use threads within Java programs. This includes developers who have previously used Java and written threaded programs; J2SE ...

    illy-Java.Threads.2nd.Edition

    《Java Threads 第二版》是一本非常全面且深入介绍Java线程编程的书籍,不仅涵盖了基本的概念和API使用,还深入探讨了线程同步、调度、并行化等高级话题。无论是对于初学者还是有经验的开发者来说,都是一本不可多得...

    Programming.With.Posix.Threads David R. Butenhof-1997.pdf

    英文版 unix, linux 学习多线程编程的好书

    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....

    Java.Threads.3rd.Edition.chm

    介绍java线程的一本很不错的书籍.英文.chm格式,对初学线程有比较大的帮助

    Node.js-一个能在单独的线程中执行Node.js函数的零依赖库

    为了解决这个问题,Node.js引入了工作线程(Worker Threads)模块,允许开发者在额外的线程中执行计算密集型任务,从而避免阻塞主线程。 描述中的“一个能在单独的线程中执行Node.js函数的零依赖库”进一步强调了该...

    python使用threading获取线程函数返回值的实现方法

    self.threads.append(t) for thread_obj in self.threads: thread_obj.start() for thread_obj in self.threads: thread_obj.join() def trace_func(self, func, *args): result = func(*args) with self....

    (中英文)-POSIX多线程程序设计-Programming with POSIX Threads

    《Programming with POSIX Threads》是David Butenhof撰写的一本经典教程,主要针对Unix/Linux环境下的多线程程序设计。本书深入浅出地介绍了POSIX线程(也称为pthreads)API,是理解并掌握多线程编程的重要参考资料...

    [DDU].Java.Threads,.Third.Edition.chm

    java中 關於 thread的使用和一些範例~

    快速了解Boost.Asio 的多线程模型

    * threads_:一个 vector,存储了多个线程对象,每个线程对象负责运行一个 io_service。 * nextIOService_:一个 size_t 变量,记录当前要返回的 io_service 的索引。 知识点4:AsioIOServicePool 的使用 ...

    CountThree.zip_threads

    在Java编程语言中,线程(Threads)是并发执行任务的基本单元。`CountThree.zip_threads`这个文件可能是一个示例项目,它展示了如何在Java中创建和管理线程,特别是与计数3秒相关的场景。这可能是为了演示线程同步、...

    curl-7.37.0简单下载多线程下载断点续载范例_非常简洁

    这个示例展示了如何使用 libcurl 创建多线程下载任务,每个 `easy_handle` 表示一个线程,通过 `curl_multi_perform` 进行同步执行。 总结,`curl` 是一个强大且灵活的工具,提供了丰富的功能来处理网络数据传输。...

    3_threads_write_file.rar_linux综合_threads_线程综合例子

    本示例"3_threads_write_file.rar"是关于在Linux下如何使用线程进行文件写入操作的一个综合实例。这个例子将帮助你深入理解和掌握线程在实际编程中的应用。 首先,我们要了解Linux下的线程API,它主要基于POSIX标准...

    Java-Threads.rar_threads

    这本书的第三版(ava Threads, 3rd Edit.chm)详细介绍了如何在Java应用程序中有效地利用多核处理器,提高程序的性能和响应速度。 在Java中,线程是程序执行的最小单元,它允许一个应用程序同时执行多个任务。通过...

Global site tag (gtag.js) - Google Analytics