Java 5.0 多线程编程实践(2)

服务任务

服务线程ServiceThread维护一个count来记录服务线程被调用的次数。每当服务任务被调用一次时,count的值自增1,因此ServiceThread提供一个increaseCount和getCount的方法,分别将count值自增1和取得该count值。由于可能多个线程存在竞争,同时访问count,因此需要加锁机制,在Java 5之前,我们只能使用synchronized来锁定。Java 5中引入了性能更加粒度更细的重入锁ReentrantLock。我们使用ReentrantLock保证代码线程安全。下面是具体代码:

private static ReentrantLock lock = new ReentrantLock ();
 
 private static int count = 0;
 
 private int getCount(){
 
 int ret = 0;

  try{
 
 lock.lock();
 
 ret = count;
 
 }finally{
 
 lock.unlock();
 
 }
 
 return ret;
 
 }
 
 private void increaseCount(){
 
 try{
 
lock.lock();
 
 ++count;
 
 }finally{
 
 lock.unlock();
 
 }
 
}

服务线程在开始给客户端打印一个欢迎信息,

increaseCount();

  int curCount = getCount();

 helloString = "hello, id = " + curCount+"\r\n";

 dos = new DataOutputStream(connectedSocket.getOutputStream());
 
  dos.write(helloString.getBytes());

然后使用ExecutorService的submit方法提交一个Callable的任务,返回一个Future接口的引用。这种做法对费时的任务非常有效,submit任务之后可以继续执行下面的代码,然后在适当的位置可以使用Future的get方法来获取结果,如果这时候该方法已经执行完毕,则无需等待即可获得结果,如果还在执行,则等待到运行完毕。

ExecutorService executor = Executors.newSingleThreadExecutor();
 
 Future future = executor.submit(new TimeConsumingTask());
 
 dos.write("let's do soemthing other".getBytes());
 
 String result = future.get();

  dos.write(result.getBytes());
 
 //其中TimeConsumingTask实现了Callable接口
 
 class TimeConsumingTask implements Callable {
 
 public String call() throws Exception {
 
 System.out.println("It's a time-consuming task,
       you'd better retrieve your result in the furture");
 
 return "ok, here's the result: It takes me lots of time to produce this result";
 
 }
 
 }

这里使用了Java 5的另外一个新特性泛型,声明TimeConsumingTask的时候使用了String做为类型参数。必须实现Callable接口的call函数,其作用类似与Runnable中的run函数,在call函数里写入要执行的代码,其返回值类型等同于在类声明中传入的类型值。在这段程序中,我们提交了一个Callable的任务,然后程序不会堵塞,而是继续执行dos.write("let's do soemthing other".getBytes());当程序执行到String result = future.get()时如果call函数已经执行完毕,则取得返回值,如果还在执行,则等待其执行完毕。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值