`
wangxiaohigh
  • 浏览: 1428409 次
文章分类
社区版块
存档分类
最新评论

Java 5.0多线程编程

 
阅读更多
<style type="text/css"> v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} w/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);}</style> <style type="text/css"> <!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Arial Narrow"; panose-1:2 11 5 6 2 2 2 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:647 0 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt; mso-fareast-language:ZH-CN;} h1 {mso-style-next:Normal; margin-top:12.0pt; margin-right:0cm; margin-bottom:3.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; page-break-after:avoid; mso-outline-level:1; font-size:16.0pt; font-family:Arial; mso-font-kerning:16.0pt; mso-fareast-language:ZH-CN;} h3 {mso-style-next:Normal; margin-top:12.0pt; margin-right:0cm; margin-bottom:3.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; page-break-after:avoid; mso-outline-level:3; font-size:13.0pt; font-family:Arial; mso-font-kerning:1.0pt; mso-fareast-language:ZH-CN;} p.MsoCaption, li.MsoCaption, div.MsoCaption {mso-style-next:Normal; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:11.0pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt; mso-fareast-language:ZH-CN; font-weight:bold; font-style:italic;} p.MsoBodyText, li.MsoBodyText, div.MsoBodyText {margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt; mso-fareast-language:ZH-CN;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} p.Default, li.Default, div.Default {mso-style-name:Default; mso-style-parent:""; margin-top:7.2pt; margin-right:0cm; margin-bottom:0cm; margin-left:0cm; margin-bottom:.0001pt; line-height:85%; mso-pagination:widow-orphan; mso-layout-grid-align:none; text-autospace:none; font-size:24.0pt; font-family:"Arial Narrow"; mso-fareast-font-family:"Times New Roman"; mso-bidi-font-family:"Times New Roman"; color:black; font-weight:bold;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:204754138; mso-list-type:hybrid; mso-list-template-ids:198206660 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l0:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l1 {mso-list-id:440534938; mso-list-type:hybrid; mso-list-template-ids:-2110865518 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l1:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l2 {mso-list-id:443423136; mso-list-type:hybrid; mso-list-template-ids:1839120510 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l2:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l3 {mso-list-id:450395124; mso-list-type:hybrid; mso-list-template-ids:1022283076 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l3:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l4 {mso-list-id:488789348; mso-list-type:hybrid; mso-list-template-ids:1155273776 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l4:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l5 {mso-list-id:597561994; mso-list-type:hybrid; mso-list-template-ids:-521388254 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l5:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l6 {mso-list-id:629828310; mso-list-type:hybrid; mso-list-template-ids:-589373002 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l6:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l7 {mso-list-id:892157281; mso-list-type:hybrid; mso-list-template-ids:-342854054 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l7:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l8 {mso-list-id:1307007902; mso-list-type:hybrid; mso-list-template-ids:812830612 67698703 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l8:level1 {mso-level-tab-stop:41.05pt; mso-level-number-position:left; margin-left:41.05pt; text-indent:-18.0pt;} @list l9 {mso-list-id:1337267957; mso-list-type:hybrid; mso-list-template-ids:-790192174 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l9:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l10 {mso-list-id:1357852085; mso-list-type:hybrid; mso-list-template-ids:1770665942 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l10:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l11 {mso-list-id:1474905401; mso-list-type:hybrid; mso-list-template-ids:-1761573404 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l11:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l12 {mso-list-id:1482308069; mso-list-type:hybrid; mso-list-template-ids:-1613877226 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l12:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l13 {mso-list-id:1565795611; mso-list-type:hybrid; mso-list-template-ids:-2068790630 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l13:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l14 {mso-list-id:1692993374; mso-list-type:hybrid; mso-list-template-ids:-1502572764 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l14:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l15 {mso-list-id:1722439826; mso-list-type:hybrid; mso-list-template-ids:1461224996 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l15:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} @list l16 {mso-list-id:1881476933; mso-list-type:hybrid; mso-list-template-ids:-703454284 67698703 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l16:level1 {mso-level-tab-stop:41.05pt; mso-level-number-position:left; margin-left:41.05pt; text-indent:-18.0pt;} @list l17 {mso-list-id:1943301886; mso-list-type:hybrid; mso-list-template-ids:-1623291660 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l17:level1 {mso-level-number-format:bullet; mso-level-text:/F0B7; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; font-family:Symbol;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --></style>

李涛,Sun中国工程研究院工程师

概述

1:三个新加的多线程包

2Callable Future接口

3:新的线程执行架构

4LockersCondition接口

5: Synchronizer:同步装置

6: BlockingQueue接口

7Atomics 原子级变量

8Concurrent Collections 共点聚集

概述

Java1995年面世以来得到了广泛得一个运用,但是对多线程编程的支持Java很长时间一直停留在初级阶段。在Java 5.0之前Java里的多线程编程主要是通过Thread类,Runnable接口,Object对象中的wait() notify() notifyAll()等方法和synchronized关键词来实现的。这些工具虽然能在大多数情况下解决对共享资源的管理和线程间的调度,但存在以下几个问题

1. 过于原始,拿来就能用的功能有限,即使是要实现简单的多线程功能也需要编写大量的代码。这些工具就像汇编语言一样难以学习和使用,比这更糟糕的是稍有不慎它们还可能被错误地使用,而且这样的错误很难被发现。

2. 如果使用不当,会使程序的运行效率大大降低。

3. 为了提高开发效率,简化编程,开发人员在做项目的时候往往需要写一些共享的工具来实现一些普遍适用的功能。但因为没有规范,相同的工具会被重复地开发,造成资源浪费。

4. 因为锁定的功能是通过Synchronized来实现的,这是一种块结构,只能对代码中的一段代码进行锁定,而且锁定是单一的。如以下代码所示:

synchronizedlock{

//执行对共享资源的操作

……

}

一些复杂的功能就很难被实现。比如说如果程序需要取得lock Alock B来进行操作1,然后需要取得lock C并且释放lock A来进行操作2Java 5.0之前的多线程框架就显得无能为力了。

因为这些问题,程序员对旧的框架一直颇有微词。这种情况一直到Java 5.0才有较大的改观,一系列的多线程工具包被纳入了标准库文件。这些工具包括了一个新的多线程程序的执行框架,使编程人员可方便地协调和调度线程的运行,并且新加入了一些高性能的常用的工具,使程序更容易编写,运行效率更高。本文将分类并结合例子来介绍这些新加的多线程工具。

在我们开始介绍Java 5.0里的新Concurrent工具前让我们先来看一下一个用旧的多线程工具编写的程序,这个程序里有一个Server线程,它需要启动两个ComponentServer线程需等到Component线程完毕后再继续。相同的功能在Synchronizer一章里用新加的工具CountDownLatch有相同的实现。两个程序,孰优孰劣,哪个程序更容易编写,哪个程序更容易理解,相信大家看过之后不难得出结论。

public class ServerThread {

Object concLock = new Object();

int count = 2;

public void runTwoThreads() {

//启动两个线程去初始化组件

new Thread(new ComponentThread1(this)).start();

new Thread(new ComponentThread1(this)).start();

// Wait for other thread

while(count != 0) {

synchronized(concLock) {

try {

concLock.wait();

System.out.println("Wake up.");

} catch (InterruptedException ie) { //处理异常}

}

}

System.out.println("Server is up.");

}

public void callBack() {

synchronized(concLock) {

count--;

concLock.notifyAll();

}

}

public static void main(String[] args){

ServerThread server = new ServerThread();

server.runTwoThreads();

}

}

public class ComponentThread1 implements Runnable {

private ServerThread server;

public ComponentThread1(ServerThread server) {

this.server = server;

}

public void run() {

//做组件初始化的工作

System.out.println("Do component initialization.");

server.callBack();

}

}

1:三个新加的多线程包

Java 5.0里新加入了三个多线程包:java.util.concurrent, java.util.concurrent.atomic, java.util.concurrent.locks.

  • java.util.concurrent包含了常用的多线程工具,是新的多线程工具的主体。

  • java.util.concurrent.atomic包含了不用加锁情况下就能改变值的原子变量,比如说AtomicInteger提供了addAndGet()方法。AddGet是两个不同的操作,为了保证别的线程不干扰,以往的做法是先锁定共享的变量,然后在锁定的范围内进行两步操作。但用AtomicInteger.addAndGet()就不用担心锁定的事了,其内部实现保证了这两步操作是在原子量级发生的,不会被别的线程干扰。

  • java.util.concurrent.locks包包含锁定的工具。

2Callable Future接口

Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务。CallableRunnable有几点不同:

  • Callable规定的方法是call(),而Runnable规定的方法是run().

  • Callable的任务执行后可返回值,而Runnable的任务是不能返回值的。

  • call()方法可抛出异常,而run()方法是不能抛出异常的。

  • 运行Callable任务可拿到一个Future对象,通过Future对象可了解任务执行情况,可取消任务的执行,还可获取任务执行的结果。

以下是Callable的一个例子:

public class DoCallStuff implements Callable<String>{ // *1

private int aInt;

public DoCallStuff(int aInt) {

this.aInt = aInt;

}

public String call() throws Exception { //*2

boolean resultOk = false;

if(aInt == 0){

resultOk = true;

} else if(aInt == 1){

while(true){ //infinite loop

System.out.println("looping....");

Thread.sleep(3000);

}

} else {

throw new Exception("Callable terminated with Exception!"); //*3

}

if(resultOk){

return "Task done.";

} else {

return "Task failed";

}

}

}

*1: 名为DoCallStuff类实现了Callable<String>String将是call方法的返回值类型。例子中用了String,但可以是任何Java类。

*2: call方法的返回值类型为String,这是和类的定义相对应的。并且可以抛出异常。

*3: call方法可以抛出异常,如加重的斜体字所示。

以下是调用DoCallStuff的主程序。

import java.util.concurrent.ExecutionException;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

public class Executor {

public static void main(String[] args){

//*1

DoCallStuff call1 = new DoCallStuff(0);

DoCallStuff call2 = new DoCallStuff(1);

DoCallStuff call3 = new DoCallStuff(2);

//*2

ExecutorService es = Executors.newFixedThreadPool(3);

//*3

Future<String> future1 = es.submit(call1);

Future<String> future2 = es.submit(call2);

Future<String> future3 = es.submit(call3);

try {

//*4

System.out.println(future1.get());

//*5

Thread.sleep(3000);

System.out.println("Thread 2 terminated? :" + future2.cancel(true));

//*6

System.out.println(future3.get());

} catch (ExecutionException ex) {

ex.printStackTrace();

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

*1: 定义了几个任务

*2: 初始了任务执行工具。任务的执行框架将会在后面解释。

*3: 执行任务,任务启动时返回了一个Future对象,如果想得到任务执行的结果或者是异常可对这个Future对象进行操作。Future所含的值必须跟Callable所含的值对映,比如说例子中Future<String>对印Callable<String>

*4: 任务1正常执行完毕,future1.get()会返回线程的值

*5: 任务2在进行一个死循环,调用future2.cancel(true)来中止此线程。传入的参数标明是否可打断线程,true表明可以打断。

*6: 任务3抛出异常,调用future3.get()时会引起异常的抛出。

运行Executor会有以下运行结果:

looping....

Task done. //*1

looping....

looping....//*2

looping....

looping....

looping....

looping....

Thread 2 terminated? :true //*3

//*4

java.util.concurrent.ExecutionException: java.lang.Exception: Callable terminated with Exception!

at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:205)

at java.util.concurrent.FutureTask.get(FutureTask.java:80)

at concurrent.Executor.main(Executor.java:43)

…….

*1: 任务1正常结束

*2: 任务2是个死循环,这是它的打印结果

*3: 指示任务2被取消

*4: 在执行future3.get()时得到任务3抛出的异常

3:新的任务执行架构

Java 5.0之前启动一个任务是通过调用Thread类的start()方法来实现的,任务的提于交和执行是同时进行的,如果你想对任务的执行进行调度或是控制同时执行的线程数量就需要额外编写代码来完成。5.0里提供了一个新的任务执行架构使你可以轻松地调度和控制任务的执行,并且可以建立一个类似数据库连接池的线程池来执行任务。这个架构主要有三个接口和其相应的具体类组成。这三个接口是Executor, ExecutorServiceScheduledExecutorService,让我们先用一个图来显示它们的关系:

<shapetype id="_x0000_t75"><stroke></stroke><formulas><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f><f></f></formulas><path></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape style="width: 359.25pt; height: 213.75pt;" id="_x0000_i1025"><imagedata o:title=""></imagedata></shape>

图的左侧是接口,图的右侧是这些接口的具体类。注意Executor是没有直接具体实现的。

Executor接口:

是用来执行Runnable任务的,它只定义一个方法:

  • execute(Runnable command):执行Ruannable类型的任务

ExecutorService接口:

ExecutorService继承了Executor的方法,并提供了执行Callable任务和中止任务执行的服务,其定义的方法主要有:

  • submit(task):可用来提交CallableRunnable任务,并返回代表此任务的Future对象

  • invokeAll(collection of tasks):批处理任务集合,并返回一个代表这些任务的Future对象集合

  • shutdown():在完成已提交的任务后关闭服务,不再接受新任务

  • shutdownNow():停止所有正在执行的任务并关闭服务。

  • isTerminated():测试是否所有任务都执行完毕了。

  • isShutdown():测试是否该ExecutorService已被关闭

ScheduledExecutorService接口

ExecutorService的基础上,ScheduledExecutorService提供了按时间安排执行任务的功能,它提供的方法主要有:

  • schedule(task, initDelay): 安排所提交的CallableRunnable任务在initDelay指定的时间后执行。

  • scheduleAtFixedRate():安排所提交的Runnable任务按指定的间隔重复执行

  • scheduleWithFixedDelay():安排所提交的Runnable任务在每次执行完后,等待delay所指定的时间后重复执行。

代码:ScheduleExecutorService的例子

public class ScheduledExecutorServiceTest {

public static void main(String[] args)

throws InterruptedException, ExecutionException{

//*1

ScheduledExecutorService service = Executors.newScheduledThreadPool(2);

//*2

Runnable task1 = new Runnable() {

public void run() {

System.out.println("Task repeating.");

}

};

//*3

final ScheduledFuture future1 =

service.scheduleAtFixedRate(task1, 0, 1, TimeUnit.SECONDS);

//*4

ScheduledFuture<String> future2 = service.schedule(new Callable<String>(){

public String call(){

future1.cancel(true);

return "task cancelled!";

}

}, 5, TimeUnit.SECONDS);

System.out.println(future2.get());

//*5

service.shutdown();

}

}

这个例子有两个任务,第一个任务每隔一秒打印一句“Task repeating,第二个任务在5秒钟后取消第一个任务。

*1: 初始化一个ScheduledExecutorService对象,这个对象的线程池大小为2

*2: 用内函数的方式定义了一个Runnable任务。

*3: 调用所定义的ScheduledExecutorService对象来执行任务,任务每秒执行一次。能重复执行的任务一定是Runnable类型。注意我们可以用TimeUnit来制定时间单位,这也是Java 5.0里新的特征,5.0以前的记时单位是微秒,现在可精确到奈秒。

*4: 调用ScheduledExecutorService对象来执行第二个任务,第二个任务所作的就是在5秒钟后取消第一个任务。

*5: 关闭服务。

Executors

虽然以上提到的接口有其实现的具体类,但为了方便Java 5.0建议使用Executors的工具类来得到Executor接口的具体对象,需要注意的是Executors是一个类,不是Executor的复数形式。Executors提供了以下一些static的方法:

  • callable(Runnable task): Runnable的任务转化成Callable的任务

  • newSingleThreadExecutor: 产生一个ExecutorService对象,这个对象只有一个线程可用来执行任务,若任务多于一个,任务将按先后顺序执行。

  • newCachedThreadPool(): 产生一个ExecutorService对象,这个对象带有一个线程池,线程池的大小会根据需要调整,线程执行完任务后返回线程池,供执行下一次任务使用。

  • newFixedThreadPool(int poolSize):产生一个ExecutorService对象,这个对象带有一个大小为poolSize的线程池,若任务数量大于poolSize,任务会被放在一个queue里顺序执行。

  • newSingleThreadScheduledExecutor:产生一个ScheduledExecutorService对象,这个对象的线程池大小为1,若任务多于一个,任务将按先后顺序执行。

  • newScheduledThreadPool(int poolSize): 产生一个ScheduledExecutorService对象,这个对象的线程池大小为poolSize,若任务数量大于poolSize,任务会在一个queue里等待执行

以下是得到和使用ExecutorService的例子:

代码:如何调用Executors来获得各种服务对象

//Single Threaded ExecutorService

ExecutorService singleThreadeService = Executors.newSingleThreadExecutor();

//Cached ExecutorService

ExecutorService cachedService = Executors.newCachedThreadPool();

//Fixed number of ExecutorService

ExecutorService fixedService = Executors.newFixedThreadPool(3);

//Single ScheduledExecutorService

ScheduledExecutorService singleScheduledService =

Executors.newSingleThreadScheduledExecutor();

//Fixed number of ScheduledExecutorService

ScheduledExecutorService fixedScheduledService =

Executors.newScheduledThreadPool(3);

4LockersCondition接口

在多线程编程里面一个重要的概念是锁定,如果一个资源是多个线程共享的,为了保证数据的完整性,在进行事务性操作时需要将共享资源锁定,这样可以保证在做事务性操作时只有一个线程能对资源进行操作,从而保证数据的完整性。在5.0以前,锁定的功能是由Synchronized关键字来实现的,这样做存在几个问题:

  • 每次只能对一个对象进行锁定。若需要锁定多个对象,编程就比较麻烦,一不小心就会出现死锁现象。

  • 如果线程因拿不到锁定而进入等待状况,是没有办法将其打断的

Java 5.0里出现两种锁的工具可供使用,下图是这两个工具的接口及其实现:

<shape style="width: 242.25pt; height: 176.25pt;" id="_x0000_i1026"><imagedata o:title=""></imagedata></shape>

Lock接口

ReentrantLockLock的具体类,Lock提供了以下一些方法:

  • lock(): 请求锁定,如果锁已被别的线程锁定,调用此方法的线程被阻断进入等待状态。

  • tryLock():如果锁没被别的线程锁定,进入锁定状态,并返回true。若锁已被锁定,返回false,不进入等待状态。此方法还可带时间参数,如果锁在方法执行时已被锁定,线程将继续等待规定的时间,若还不行才返回false

  • unlock():取消锁定,需要注意的是Lock不会自动取消,编程时必须手动解锁。

代码:

//生成一个锁

Lock lock = new ReentrantLock();

public void accessProtectedResource() {

lock.lock(); //取得锁定

try {

//对共享资源进行操作

} finally {

//一定记着把锁取消掉,锁本身是不会自动解锁的

lock.unlock();

}

}

ReadWriteLock接口

为了提高效率有些共享资源允许同时进行多个读的操作,但只允许一个写的操作,比如一个文件,只要其内容不变可以让多个线程同时读,不必做排他的锁定,排他的锁定只有在写的时候需要,以保证别的线程不会看到数据不完整的文件。ReadWriteLock可满足这种需要。ReadWriteLock内置两个Lock,一个是读的Lock,一个是写的Lock。多个线程可同时得到读的Lock,但只有一个线程能得到写的Lock,而且写的Lock被锁定后,任何线程都不能得到LockReadWriteLock提供的方法有:

  • readLock(): 返回一个读的lock

  • writeLock(): 返回一个写的lock, lock是排他的。

ReadWriteLock的例子:

public class FileOperator{

//初始化一个ReadWriteLock

ReadWriteLock lock = new ReentrantReadWriteLock();

public String read() {

//得到readLock并锁定

Lock readLock = lock.readLock();

readLock.lock();

try {

//做读的工作

return "Read something";

} finally {

readLock.unlock();

}

}

public void write(String content) {

//得到writeLock并锁定

Lock writeLock = lock.writeLock();

writeLock.lock();

try {

//做读的工作

} finally {

writeLock.unlock();

}

}

}

需要注意的是ReadWriteLock提供了一个高效的锁定机理,但最终程序的运行效率是和程序的设计息息相关的,比如说如果读的线程和写的线程同时在等待,要考虑是先发放读的lock还是先发放写的lock。如果写发生的频率不高,而且快,可以考虑先给写的lock。还要考虑的问题是如果一个写正在等待读完成,此时一个新的读进来,是否要给这个新的读发锁,如果发了,可能导致写的线程等很久。等等此类问题在编程时都要给予充分的考虑。

Condition接口:

有时候线程取得lock后需要在一定条件下才能做某些工作,比如说经典的ProducerConsumer问题,Consumer必须在篮子里有苹果的时候才能吃苹果,否则它必须暂时放弃对篮子的锁定,等到Producer往篮子里放了苹果后再去拿来吃。而Producer必须等到篮子空了才能往里放苹果,否则它也需要暂时解锁等Consumer把苹果吃了才能往篮子里放苹果。在Java 5.0以前,这种功能是由Object类的wait(), notify()notifyAll()等方法实现的,在5.0里面,这些功能集中到了Condition这个接口来实现,Condition提供以下方法:

  • await():使调用此方法的线程放弃锁定,进入睡眠直到被打断或被唤醒。

  • signal(): 唤醒一个等待的线程

  • signalAll():唤醒所有等待的线程

Condition的例子:

public class Basket {

Lock lock = new ReentrantLock();

//产生Condition对象

Condition produced = lock.newCondition();

Condition consumed = lock.newCondition();

boolean available = false;

public void produce() throws InterruptedException {

lock.lock();

try {

if(available){

consumed.await(); //放弃lock进入睡眠

}

/*生产苹果*/

System.out.println("Apple produced.");

available = true;

produced.signal(); //发信号唤醒等待这个Condition的线程

} finally {

lock.unlock();

}

}

public void consume() throws InterruptedException {

lock.lock();

try {

if(!available){

produced.await();//放弃lock进入睡眠

}

/*吃苹果*/

System.out.println("Apple consumed.");

available = false;

consumed.signal();//发信号唤醒等待这个Condition的线程

} finally {

lock.unlock();

}

}

}

ConditionTester:

public class ConditionTester {

public static void main(String[] args) throws InterruptedException{

final Basket basket = new Basket();

//定义一个producer

Runnable producer = new Runnable() {

public void run() {

try {

basket.produce();

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

};

//定义一个consumer

Runnable consumer = new Runnable() {

public void run() {

try {

basket.consume();

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

};

//各产生10consumerproducer

ExecutorService service = Executors.newCachedThreadPool();

for(int i=0; i < 10; i++)

service.submit(consumer);

Thread.sleep(2000);

for(int i=0; i<10; i++)

service.submit(producer);

service.shutdown();

}

}

5: Synchronizer:同步装置

Java 5.0里新加了4个协调线程间进程的同步装置,它们分别是Semaphore, CountDownLatch, CyclicBarrierExchanger.

Semaphore:

用来管理一个资源池的工具,Semaphore可以看成是个通行证,线程要想从资源池拿到资源必须先拿到通行证,Semaphore提供的通行证数量和资源池的大小一致。如果线程暂时拿不到通行证,线程就会被阻断进入等待状态。以下是一个例子:

public class Pool {

ArrayList<String> pool = null;

Semaphore pass = null;

public Pool(int size){

//初始化资源池

pool = new ArrayList<String>();

for(int i=0; i<size; i++){

pool.add("Resource "+i);

}

//Semaphore的大小和资源池的大小一致

pass = new Semaphore(size);

}

public String get() throws InterruptedException{

//获取通行证,只有得到通行证后才能得到资源

pass.acquire();

return getResource();

}

public void put(String resource){

//归还通行证,并归还资源

pass.release();

releaseResource(resource);

}

private synchronized String getResource() {

String result = pool.get(0);

pool.remove(0);

System.out.println("Give out "+result);

return result;

}

private synchronized void releaseResource(String resource) {

System.out.println("return "+resource);

pool.add(resource);

}

}

SemaphoreTest:

public class SemaphoreTest {

public static void main(String[] args){

final Pool aPool = new Pool(2);

Runnable worker = new Runnable() {

public void run() {

String resource = null;

try {

//取得resource

resource = aPool.get();

} catch (InterruptedException ex) {

ex.printStackTrace();

}

//resource做工作

System.out.println("I worked on "+resource);

//归还resource

aPool.put(resource);

}

};

ExecutorService service = Executors.newCachedThreadPool();

for(int i=0; i<20; i++){

service.submit(worker);

}

service.shutdown();

}

}

CountDownLatch:

CountDownLatch是个计数器,它有一个初始数,等待这个计数器的线程必须等到计数器倒数到零时才可继续。比如说一个Server启动时需要初始化4个部件,Server可以同时启动4个线程去初始化这4个部件,然后调用CountDownLatch(4).await()阻断进入等待,每个线程完成任务后会调用一次CountDownLatch.countDown()来倒计数, 4个线程都结束时CountDownLatch的计数就会降低为0,此时Server就会被唤醒继续下一步操作。CountDownLatch的方法主要有:

  • await():使调用此方法的线程阻断进入等待

  • countDown(): 倒计数,将计数值减1

  • getCount(): 得到当前的计数值

CountDownLatch的例子:一个server调了三个ComponentThread分别去启动三个组件,然后server等到组件都启动了再继续。

public class Server {

public static void main(String[] args) throws InterruptedException{

System.out.println("Server is starting.");

//初始化一个初始值为3CountDownLatch

CountDownLatch latch = new CountDownLatch(3);

//3个线程分别去启动3个组件

ExecutorService service = Executors.newCachedThreadPool();

service.submit(new ComponentThread(latch, 1));

service.submit(new ComponentThread(latch, 2));

service.submit(new ComponentThread(latch, 3));

service.shutdown();

//进入等待状态

latch.await();

//当所需的三个组件都完成时,Server就可继续了

System.out.println("Server is up!");

}

}

public class ComponentThread implements Runnable{

CountDownLatch latch;

int ID;

/** Creates a new instance of ComponentThread */

public ComponentThread(CountDownLatch latch, int ID) {

this.latch = latch;

this.ID = ID;

}

public void run() {

System.out.println("Component "+ID + " initialized!");

//将计数减一

latch.countDown();

}

}

运行结果:

Server is starting.

Component 1 initialized!

Component 3 initialized!

Component 2 initialized!

Server is up!

CyclicBarrier:

CyclicBarrier类似于CountDownLatch也是个计数器,不同的是CyclicBarrier数的是调用了CyclicBarrier.await()进入等待的线程数,当线程数达到了CyclicBarrier初始时规定的数目时,所有进入等待状态的线程被唤醒并继续。CyclicBarrier就象它名字的意思一样,可看成是个障碍,所有的线程必须到齐后才能一起通过这个障碍。CyclicBarrier初始时还可带一个Runnable的参数,此Runnable任务在CyclicBarrier的数目达到后,所有其它线程被唤醒前被执行。

CyclicBarrier提供以下几个方法:

  • await():进入等待

  • getParties():返回此barrier需要的线程数

  • reset():将此barrier重置

以下是使用CyclicBarrier的一个例子:两个线程分别在一个数组里放一个数,当这两个线程都结束后,主线程算出数组里的数的和(这个例子比较无聊,我没有想到更合适的例子)

public class MainThread {

public static void main(String[] args)

throws InterruptedException, BrokenBarrierException, TimeoutException{

final int[] array = new int[2];

CyclicBarrier barrier = new CyclicBarrier(2,

new Runnable() {//在所有线程都到达Barrier时执行

public void run() {

System.out.println("Total is:"+(array[0]+array[1]));

}

});

//启动线程

new Thread(new ComponentThread(barrier, array, 0)).start();

new Thread(new ComponentThread(barrier, array, 1)).start();

}

}

public class ComponentThread implements Runnable{

CyclicBarrier barrier;

int ID;

int[] array;

public ComponentThread(CyclicBarrier barrier, int[] array, int ID) {

this.barrier = barrier;

this.ID = ID;

this.array = array;

}

public void run() {

try {

array[ID] = new Random().nextInt();

System.out.println(ID+ " generates:"+array[ID]);

//该线程完成了任务等在Barrier

barrier.await();

} catch (BrokenBarrierException ex) {

ex.printStackTrace();

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

Exchanger:

顾名思义Exchanger让两个线程可以互换信息。用一个例子来解释比较容易。例子中服务生线程往空的杯子里倒水,顾客线程从装满水的杯子里喝水,然后通过Exchanger双方互换杯子,服务生接着往空杯子里倒水,顾客接着喝水,然后交换,如此周而复始。

class FillAndEmpty {

//初始化一个Exchanger,并规定可交换的信息类型是DataCup

Exchanger<Cup> exchanger = new Exchanger();

Cup initialEmptyCup = ...; //初始化一个空的杯子

Cup initialFullCup = ...; //初始化一个装满水的杯子

//服务生线程

class Waiter implements Runnable {

public void run() {

Cup currentCup = initialEmptyCup;

try {

//往空的杯子里加水

currentCup.addWater();

//杯子满后和顾客的空杯子交换

currentCup = exchanger.exchange(currentCup);

} catch (InterruptedException ex) { ... handle ... }

}

}

//顾客线程

class Customer implements Runnable {

public void run() {

DataCup currentCup = initialFullCup;

try {

//把杯子里的水喝掉

currentCup.drinkFromCup();

//将空杯子和服务生的满杯子交换

currentCup = exchanger.exchange(currentCup);

} catch (InterruptedException ex) { ... handle ...}

}

}

void start() {

new Thread(new Waiter()).start();

new Thread(new Customer()).start();

}

}

6: BlockingQueue接口

BlockingQueue是一种特殊的Queue,若BlockingQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态直到BlocingkQueue进了新货才会被唤醒。同样,如果BlockingQueue是满的任何试图往里存东西的操作也会被阻断进入等待状态,直到BlockingQueue里有新的空间才会被唤醒继续操作。BlockingQueue提供的方法主要有:

  • add(anObject): anObject加到BlockingQueue里,如果BlockingQueue可以容纳返回true,否则抛出IllegalStateException异常。

  • offer(anObject):把anObject加到BlockingQueue里,如果BlockingQueue可以容纳返回true,否则返回false

  • put(anObject):把anObject加到BlockingQueue里,如果BlockingQueue没有空间,调用此方法的线程被阻断直到BlockingQueue里有新的空间再继续。

  • poll(time):取出BlockingQueue里排在首位的对象,若不能立即取出可等time参数规定的时间。取不到时返回null

  • take():取出BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态直到BlockingQueue有新的对象被加入为止。

根据不同的需要BlockingQueue4种具体实现:

  • ArrayBlockingQueue:规定大小的BlockingQueue,其构造函数必须带一个int参数来指明其大小。其所含的对象是以FIFO(先入先出)顺序排序的。

  • LinkedBlockingQueue:大小不定的BlockingQueue,若其构造函数带一个规定大小的参数,生成的BlockingQueue有大小限制,若不带大小参数,所生成的BlockingQueue的大小由Integer.MAX_VALUE来决定。其所含的对象是以FIFO(先入先出)顺序排序的。LinkedBlockingQueueArrayBlockingQueue比较起来,它们背后所用的数据结构不一样,导致LinkedBlockingQueue的数据吞吐量要大于ArrayBlockingQueue,但在线程数量很大时其性能的可预见性低于ArrayBlockingQueue

  • PriorityBlockingQueue:类似于LinkedBlockingQueue,但其所含对象的排序不是FIFO,而是依据对象的自然排序顺序或者是构造函数所带的Comparator决定的顺序。

  • SynchronousQueue:特殊的BlockingQueue,对其的操作必须是放和取交替完成的。

下面是用BlockingQueue来实现ProducerConsumer的例子:

public class BlockingQueueTest {

static BlockingQueue<String> basket;

public BlockingQueueTest() {

//定义了一个大小为2BlockingQueue,也可根据需要用其他的具体类

basket = new ArrayBlockingQueue<String>(2);

}

class Producor implements Runnable {

public void run() {

while(true){

try {

//放入一个对象,若basket满了,等到basket有位置

basket.put("An apple");

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

}

class Consumer implements Runnable {

public void run() {

while(true){

try {

//取出一个对象,若basket为空,等到basket有东西为止

String result = basket.take();

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

}

public void execute(){

for(int i=0; i<10; i++){

new Thread(new Producor()).start();

new Thread(new Consumer()).start();

}

}

public static void main(String[] args){

BlockingQueueTest test = new BlockingQueueTest();

test.execute();

}

}

7Atomics 原子级变量

原子量级的变量,主要的类有AtomicBoolean, AtomicInteger, AotmicIntegerArray, AtomicLong, AtomicLongArray, AtomicReference ……。这些原子量级的变量主要提供两个方法:

  • compareAndSet(expectedValue, newValue): 比较当前的值是否等于expectedValue,若等于把当前值改成newValue,并返回true。若不等,返回false

  • getAndSet(newValue): 把当前值改为newValue,并返回改变前的值。

这些原子级变量利用了现代处理器(CPU)的硬件支持可把两步操作合为一步的功能,避免了不必要的锁定,提高了程序的运行效率。

8Concurrent Collections 共点聚集

Java的聚集框架里可以调用Collections.synchronizeCollection(aCollection)将普通聚集改变成同步聚集,使之可用于多线程的环境下。 但同步聚集在一个时刻只允许一个线程访问它,其它想同时访问它的线程会被阻断,导致程序运行效率不高。Java 5.0里提供了几个共点聚集类,它们把以前需要几步才能完成的操作合成一个原子量级的操作,这样就可让多个线程同时对聚集进行操作,避免了锁定,从而提高了程序的运行效率。Java 5.0目前提供的共点聚集类有:ConcurrentHashMap, ConcurrentLinkedQueue, CopyOnWriteArrayListCopyOnWriteArraySet.

分享到:
评论

相关推荐

    Java5.0多线程编程

    最新Java 5.0多线程编程。

    Java5.0多线程编程实践.pdf

    Java5.0多线程编程实践.pdf

    (超赞)JAVA精华之--深入JAVA API

    1.7 Java 5.0多线程编程 1.8 Java Socket编程 1.9 Java的内存泄漏 1.10 抽象类与接口的区别 1.11 Java变量类型间的相互转换 2 JAVA与WEB 2.1 JMX规范 2.1.1 JMX概述 2.1.2 设备层(Instrumentation Level) 2.1.3 ...

    JAVA SE学习精华集锦

    1.7 Java 5.0多线程编程 65 1.8 Java Socket编程 80 1.9 Java的内存泄漏 85 1.10 抽象类与接口的区别 86 1.11 Java变量类型间的相互转换 87 2 JAVA与WEB 87 2.1 JMX规范 87 2.1.1 JMX概述 87 2.1.2 设备层...

    Java精华学习资料

    Java精华学习资料 深入JAVA API 深入理解嵌套类和内部类 文件和流 java中的一些常用词汇 ...Java 5.0多线程编程 Java Socket编程 Java的内存泄漏 抽象类与接口的区别 Java变量类型间的相互转换 ……

    多线程编程(电子书)

    Java自1995年面世以来得到了广泛得一个运用,但是对多...在Java 5.0之前Java里的多线程编程主要是通过Thread类,Runnable接口,Object对象中的wait()、 notify()、 notifyAll()等方法和synchronized关键词来实现的。

    Java高手真经(编程基础卷)光盘全部源码 免积分

    javathread.zip 10.Java多线程编程(线程池、生产者消费者、存取款实例) javautil.zip 11.Java常用实体类 javaxml.zip 14.XML属性文件 第4部分(6个程序包) javagui.zip 15.Java GUI库对比实例 javaawt.zip ...

    Java高手真经(编程基础卷)光盘全部源码

    javathread.zip 10.Java多线程编程(线程池、生产者消费者、存取款实例) javautil.zip 11.Java常用实体类 javaxml.zip 14.XML属性文件 第4部分(6个程序包) javagui.zip 15.Java GUI库对比实例 javaawt.zip 16....

    基于Java的多核多线程编程技术 (2009年)

    从多核多线程的角度出发,介绍了操作系统多核的处理模式,并结合Java中的多线程技术,讨论了线程和线程池在Java中的...通过实例,展现了Java对多线程的强大支持,同时也讨论了Java5.0的一些和多线程相关的新增特性.

    java高手真经 光盘源码

    javathread.zip 10.Java多线程编程(线程池、生产者消费者、存取款实例) javautil.zip 11.Java常用实体类 javaxml.zip 14.XML属性文件 第4部分(6个程序包) javagui.zip 15.Java GUI库对比实例 javaawt.zip ...

    精通Java:JDK、数据库系统开发Web开发(实例代码)

    《精通Java:JDK、数据库系统开发Web开发》全书共分27章,内容涵盖了Java编程环境概述、基础语法、面向对象软件设计方法、线程、数据集合、网络编程、图形编程、多媒体编程以及Java Web开发。本书每一节的例子都是...

    Java优化编程(第2版)

    第12章 java多线程技术与应用性能优化 12.1 java多线程技术 12.1.1 进程与线程 12.1.2 线程的生命周期 12.2 并行任务与性能 12.2.1 并行任务与多线程 12.2.2 并行任务与死锁 12.3 线程池技术与应用性能优化 12.3.1 ...

    22 尚硅谷Java JUC线程高级视频

    教程视频:在 Java 5.0 提供了 java.util.concurrent(简称JUC)包,在此包中增加了在并发编程中很常用的工具类, 用于定义类似于线程的自定义子系统,包括线程池,异步 IO 和轻量级任务框架;还提供了设计用于多线程上下文...

    Java面试宝典Beta5.0.pdf

    java面试经常问到的问题,java的基础问题,多线程,spring mvc框架,jsp资源.Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点

Global site tag (gtag.js) - Google Analytics