以上内容均来自于本书和我的自己理解的总结
Java Threads, 3rd Edition [Book] (oreilly.com)
Atomic variables A set of classes that provide threadsafe operations without synchronization |
原子变量
提供线程安全操作而无需同步的一组类
我的理解:利用这种原子变量起到定义线程安全的变量,而且不用额外编写同步代码
最小同步实用程序
Java的新原子类类型提供了一种方法,开发者可以在必要时编写避免同步的字样(synchronization)应用程序。这可以使得程序提高更高的并发性
————————————————————————
Explicit locks
Synchronization locks that can be acquired and released programmatically
显式锁(精确锁)
可以通过编程方式获取和释放的同步锁
————————————————————————
Condition variables Variables that can be the subject of a targeted notification when certain conditions exist |
条件变量
存在特定条件时,可能成为目标通知主题的变量
The Java language has always had the capability to perform data synchronization and thread notification. However, implementation of these features was somewhat limited; you could, for example, synchronize blocks of code or entire methods but synchronizing across methods and classes required extra programming. In J2SE 5.0, explicit locks and condition variables allow you more flexibility when using these features.
Java语言一直能够执行数据同步和线程通知。然而,这些功能的实现有些有限;例如,可以同步代码块或同步整个方法,但跨整个方法和类同步需要额外编程。在J2SE 5.0中,显式锁(精确锁)和条件变量允许您在使用这些特性时具有更大的灵活性。
————————————————————————
Queues
Collection classes that are thread-aware
队列
线程能够感知并使用的一种集合类
————————————————————————————
Synchronization primitives
New classes that perform complex types of synchronization
同步关键字:
同步原语
执行复杂类型同步的新功能类
————————————————————————————
Thread pools Classes that can manage a pool of threads to run certain tasks |
线程池
可以管理一池子线程去执行特定任务们的一种类
————————————————————————————
Thread schedulers
Classes that can execute tasks at a particular point in time
线程调度程序
可以在特定时间点执行任务的类
重要的线程实用程序(线程池和时间调度器没有必要自己编写,因为已经有现成的实用程序了)
在某个时候,几乎所有编写线程程序的开发人员都需要使用基本的线程实用程序,比如线程池或调度器;他们中的许多人还需要使用高级同步原语。认识到这一事实是促使JSR-166的原因之一——在Java的早期版本中,开发自己的线程池和调度程序当然是可能的。但是考虑到线程在Java平台中的重要性,添加这些基本实用程序可以极大地提高程序员的工作效率(已经有现成的了,别自己开发,找现成类库即可搞定)。
thread可以有两种创建方式:
第一种 使用Thread class
第二种通过Runnable interface
注意:通过Runnable interface所创建的线程 (通常)需要一个thread的instance,所以我们先从Thread class 开始学起。
创建Thread
生命周期的第一个阶段是Thread的创建。thread是由Thread class的instance来表示,
所以创建thread是通过调用该class的constructor来完成。在我们的范例中使用了最简单的constructor其他 Thread class 的constructor 能够让你指定thread的名称或者
Runnable对象以作为thread的target