Concurrent (1)

来源:互联网 发布:如何评价王思聪 知乎 编辑:程序博客网 时间:2024/04/28 10:54

jsdk1.5中除了增强了socket通信外,还把Doug Leaconcurrent包添加进来,包名为java.util. concurren.* ,提高了线程处理的性能,当然jsdk1.5中还增加了对aop和泛型的支持。

 Doug Leaconcurrent包的美名是否名副其实呢?让我们来看一下:

一、ScheduledExecutor

这个工具用来做日程表时钟,可以用来做定时器,定时、延时或循环执行某个或多个任务,是ClockDaemon的改进版,它有下面一些方法:

    public ScheduledCancellable schedule(Runnable command, long delay,  TimeUnit unit)

    public ScheduledCancellable scheduleAtFixedRate(Runnable command, long initialDelay,  long period, TimeUnit unit)

public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)

public ScheduledCancellable scheduleWithFixedDelay(Runnable command, long initialDelay,  long delay, TimeUnit unit)

public void execute(Runnable command)

public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value)

public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy()

public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value)

public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy()

public void shutdown()

public List shutdownNow()

public boolean remove(Runnable task)

public BlockingQueue<Runnable> getQueue()

 

方法很多,我们只研究几个比较常用的:

public ScheduledCancellable schedule(Runnable command, long delay,  TimeUnit unit)

public ScheduledCancellable scheduleAtFixedRate(Runnable command, long initialDelay,  long period, TimeUnit unit)

public ScheduledCancellable scheduleWithFixedDelay(Runnable command, long initialDelay,  long delay, TimeUnit unit)

这三个方法是用来定时运行我们赋给它的线程command,例子:

/*

 * Created on 2004-8-25

 *

 */

package test1;

import java.util.concurrent.*;

/**  

 * @author <a href="mailto:Azure_2003@126.com">Azure</a>

 *

 */

public class Foo {

    private int number = 0;

 

    public static void main(String[] args) {

       Foo foo=new Foo();

       ScheduledExecutor schedule=new ScheduledExecutor(10);

      

     schedule.scheduleAtFixedRate(foo.new Worker(),0,1,TimeUnit.SECONDS);

    }

 

    private class Worker implements Runnable {

       public void run() {

           number = number + 1;

           System.out.println(number);

       }

    }

}

在例子中我们写了一个线程类Worker,创建了一个ScheduledExecutor对象schedule,该对象允许的最大worker线程数目为10(根据需要自定义的),我们使用了方法

public ScheduledCancellable scheduleAtFixedRate(Runnable command, long initialDelay,  long period, TimeUnit unit)

功能是让Worker线程在时间initialDelay之后,每隔period时间运行一次,TimeUnit是时间单位,TimeUnit.SECONDS的意思是单位为秒。

运行程序后我们会发现每隔1秒打印一个数字,不停的打印下去。

 

如果我们把

     schedule.scheduleAtFixedRate(foo.new Worker(),0,1,TimeUnit.SECONDS);

处改为:

schedule.schedule(foo.new Worker(),2,TimeUnit.SECONDS);

schedule.shutdown();

结果会是怎样的呢?

它会打出一个数字然后结束程序。

 

如果我们把此处改为:

schedule.scheduleWithFixedDelay(foo.new Worker(),0,1,TimeUnit.SECONDS);

它的结果没有任何变化,这两者的基本上是通用的,只是记时的问题,一个是前者的起步

间在不停的更新,后者的起步时间一直都保留着。

 

二、LinkedBlockingQueue

Concurrent包的LinkedQueue改进版,这个队列的实现使用了合成模式模拟了一个队列,对元素的存取都使用了同步,这样做的好处在于当很多线程一起使用时线程的安全性比较高,可以用来做数据共享池,缺点在于太多的同步会造成系统性能降低。

它的使用方法很多,也很简单,put(),get(),remove()等等。

 

三、ReentrantLock

ReentrantLock是可重入锁,这个工具可以代替synchronizedsynchronized是对一个方法或者一片区域加锁,如果对一个方法加锁,这样范围比较大,不如对区域加锁的性能高,ReentrantLock可以对一片区域加锁,并且可以使用它的一些方法来管理锁,它提供下面一些方法:

public void unlock()

public void lock()

public void lockInterruptibly() throws InterruptedException

public boolean tryLock()

public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException

public ConditionObject newCondition()

public boolean isFair()

public int getHoldCount()

public boolean isHeldByCurrentThread()

public boolean isLocked()

public int getQueueLength()

原创粉丝点击