Floodlight 入门 之 起步篇 - 如何使用ThreadPool

Floodlight 入门 之 起步篇 - 如何使用ThreadPool

2017-3-12

今天在写程序的时候需要用到多线程,查阅Floodlight的官方文档发现其中有ThreadPool这样一个Controller模块…瞬间不敢随便用ThreadPoolExecutor了,还是先学习一下吧。


官方对该模块的说明非常简单:

ThreadPool is a Floodlight module wrapper for a Java’s ScheduledExecutorService. It can be used to have threads be run at specific times or periodically.(ThreadPool这个Floodlight模块是Java的ScheduledExecutorService的一个封装。它可以让线程周期性或按照特定的时序运行。)

这个模块由IThreadPoolService提供,且不依赖于其他服务。ScheduledExecutorService使用的例子如下:

 import static java.util.concurrent.TimeUnit.*;
 class BeeperControl {
    private final ScheduledExecutorService scheduler =
       Executors.newScheduledThreadPool(1);

    public void beepForAnHour() {
        final Runnable beeper = new Runnable() {
                public void run() { System.out.println("beep"); }
            };
        final ScheduledFuture<?> beeperHandle =
            scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
        scheduler.schedule(new Runnable() {
                public void run() { beeperHandle.cancel(true); }
            }, 60 * 60, SECONDS);
    }
 }

然而,我看不出来这个跟IThreadPoolService和ThreadPool有什么关系啊(没有用到IThreadPoolService和ThreadPool…),于是我又查了官方的Java Doc:

下面是依赖关系:

Class ThreadPool

java.lang.Object
    net.floodlightcontroller.threadpool.ThreadPool
All Implemented Interfaces:
IFloodlightModule, IFloodlightService, IThreadPoolService

public class ThreadPool
extends java.lang.Object
implements IThreadPoolService, IFloodlightModule

然后它的成员包括:

  • protected java.util.concurrent.ScheduledExecutorService executor

  • 构造函数

    ThreadPool()

  • 方法

方法名原型详细
getModuleDependenciesjava.util.Collection> getModuleDependencies()获取模块依赖项
getModuleServicesjava.util.Collection> getModuleServices()返回模块实现的接口
getScheduledExecutorjava.util.concurrent.ScheduledExecutorService getScheduledExecutor()Get the master scheduled thread pool executor maintained by the ThreadPool provider.
getServiceImplsjava.util.Map,IFloodlightService> getServiceImpls()Instantiate (as needed) and return objects that implement each of the services exported by this module.
initvoid init(FloodlightModuleContext context)This is a hook for each module to do its internal initialization, e.g., call setService(context.getService(“Service”)) All module dependencies are resolved when this is called, but not every module is initialized.
startUpvoid startUp(FloodlightModuleContext context)This is a hook for each module to do its external initializations, e.g., register for callbacks or query for state in other modules It is expected that this function will not block and that modules that want non-event driven CPU will spawn their own threads.

- 继承的方法:clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

我们再来看看IThreadPoolService接口:

照旧,我们先看看依赖关系:

All Superinterfaces:
IFloodlightService
All Known Implementing Classes:
ThreadPool

public interface IThreadPoolService
extends IFloodlightService

然后,我们来看看它的方法

方法名原型详细
getScheduledExecutorjava.util.concurrent.ScheduledExecutorService getScheduledExecutor()Get the master scheduled thread pool executor maintained by the ThreadPool provider.

好的,现在我们知道了,这里的ThreadPool只是对ScheduledExecutorService进行了封装,最后使用的还是ScheduledExecutorService对象。所以现在让我们来看看ScheduledExecutorService是什么样的:

ScheduledExecutorService的依赖关系:

All Superinterfaces:
    Executor, ExecutorService
All Known Implementing Classes:
    ScheduledThreadPoolExecutor
public interface ScheduledExecutorService
extends ExecutorService

ScheduledExecutorService的方法:

方法名原型详细
schedule ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit)
scheduleScheduledFuture schedule(Runnable command, long delay, TimeUnit unit)
scheduleAtFixedRateScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
scheduleWithFixedDelayScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)

ScheduledExecutorService继承的方法:

awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow, submit, submit, submit(Methods inherited from interface java.util.concurrent.ExecutorService)


execute(Methods inherited from interface java.util.concurrent.Executor)

官方给出的文档中主要说明也就这些了,我们现在结合Floodlight的代码来看一下,我们该如何使用这些模块和API:

这边举的例子还用到了Floodlight的另一个类SingletonTask,它可以让创建的线程只跑一次,我们来看看官方文档的说明吧(不要急):

依赖关系:

Class SingletonTask

java.lang.Object
net.floodlightcontroller.core.util.SingletonTask

public class SingletonTask
extends java.lang.Object

官方文档在开头balbala说了一大段为什么要有这个类,然后指出了这个类的一个特点:当创建的任务还没有开始时,你是没有办法创建一个新的任务的;当创建的任务运行时,它会设置一个标志位,当任务运行结束时它会重新再运行一次。

因为我们重点不在SingletonTask,所以这里仅仅简单介绍一下我们要用的方法。首先我们要看的是它的构造函数:

SingletonTask(java.util.concurrent.ScheduledExecutorService ses, java.lang.Runnable task)

我们可以看到,构造函数的第一个参数是java.util.concurrent.ScheduledExecutorService,这个我们可以通过IThreadPoolService的getScheduledExecutor方法获得。然后第二个参数是java.lang.Runnable,这个就可以放实现了Runnable接口的对象了。

构造函数搞定后,肯定还是要调用它的一些方法来控制任务的运行的。所以我们接下来看看它的方法。它的方法很简单,只有一个:

void    reschedule(long delay, java.util.concurrent.TimeUnit unit)

官方文档中对该方法的说明如下:当目前已经没有任务被调度则让当前的任务运行。如果已经有一个任务被调度了,但还没有开始,它就会取消这个任务并在给定的时间内重新调度这个任务,让它运行。如果一个任务已经开始了…这句话写的有点看不太懂( If the task is already started, it will cause the task to be rescheduled once it completes to run after delay from the time of reschedule.)我这边的翻译是如果该任务在reschedule给定的时间延迟后完成了运行,则它将会让这个任务重新被调度。而我的理解是,如果在给定时间内,当前运行的任务没有结束,它就会重新运行新的任务。

我们看看reschedule的参数,这很简单,第一个参数是延时时间的数值,第二个参数是单位。

到此为止,我们就可以完成一个简单的例子了:

class worker implement Runnable
{
public worker()
{

}

void run()
{
System.out.println("working");
}
}

SingletonTask task = new SingletonTask(IThreadPoolService.getScheduledExecutor(),new worker());
task.reschedule(500,TimeUnit.MILLISECONDS);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值