<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.tianjun</groupId><artifactId>scala-test</artifactId><version>1.0</version><properties><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target><encoding>UTF-8</encoding><scala.version>2.10.6</scala.version></properties><dependencies><dependency><groupId>org.scala-lang</groupId><artifactId>scala-library</artifactId><version>${scala.version}</version></dependency><!-- Test --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies><build><sourceDirectory>src/main/scala</sourceDirectory><testSourceDirectory>src/test/scala</testSourceDirectory><plugins><plugin><groupId>net.alchim31.maven</groupId><artifactId>scala-maven-plugin</artifactId><version>3.2.2</version><executions><execution><goals><goal>compile</goal><goal>testCompile</goal></goals><configuration><args><arg>-make:transitive</arg><arg>-dependencyfile</arg><arg>${project.build.directory}/.scala_dependencies</arg></args></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>2.4.3</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><filters><filter><artifact>*:*</artifact><excludes><exclude>META-INF/*.SF</exclude><exclude>META-INF/*.DSA</exclude><exclude>META-INF/*.RSA</exclude></excludes></filter></filters></configuration></execution></executions></plugin></plugins></build></project>
接口和样本类
package thread
/**
* Created by tianjun on 2017/8/22.
*/traitTaskEventcaseclassTaskSubmitted(name:String)extendsTaskEventcaseclassTaskSucceeded(name:String)extendsTaskEventcaseclassTaskFailed(name:String)extendsTaskEvent
任务进队列,队列的处理的抽象类
package thread
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.{LinkedBlockingDeque, BlockingQueue}
import scala.util.control.NonFatal
/**
* Created by tianjun on 2017/8/22.
*/abstractclassEventLoop[E](name: String) {privateval eventQueue: BlockingQueue[E] = new LinkedBlockingDeque[E]()
privateval stopped = new AtomicBoolean(false)
privateval eventThread = new Thread(name){
overridedef run():Unit = {
try{
while (!stopped.get()){
val event = eventQueue.take()
try{
//偏函数,某一类类型的任务交给某一类型去处理
onReceive(event)//在子类TaskProcessEventLoop中有实现,就是打印event
}catch {
case NonFatal(e) => {
try{
onError(e)
}catch {
case NonFatal(e) => println("Unexpected error in " + name,e)
}
}
}
}
}catch {
case ie:InterruptedException => // exit even if eventQuene is not emptycase NonFatal(e) => println("Unexpected error in "+name,e)
}
}
}
def start(): Unit = {
if(stopped.get()){
thrownew IllegalStateException(name+" has already been stopped")
}
onStart()
eventThread.start()
}
def stop(): Unit ={
if(stopped.compareAndSet(false,true)){
eventThread.interrupt()
var onStopCalled = falsetry{
eventThread.join()
onStopCalled = true
onStop()
}catch{
case ie: InterruptedException =>
Thread.currentThread().interrupt()
if(!onStopCalled){
onStop()
}
}
}else{
}
}
def post(event : E) : Unit = {
eventQueue.put(event)
}
def isActive: Boolean = eventThread.isAlive
protecteddef onStart(): Unit = {}
protecteddef onStop(): Unit = {}
protecteddef onReceive: PartialFunction[E,Unit]
protecteddef onError(e: Throwable): Unit
}
队列处理的实现类
package thread
/**
* Created by tianjun on 2017/8/22.
*/classTaskProcessEventLoop(name:String)extendsEventLoop[TaskEvent](name){overrideprotecteddef onReceive:PartialFunction[TaskEvent,Unit] = {
case TaskSubmitted(taskName) => println(taskName)
}
overrideprotecteddef onError(e:Throwable):Unit = {
}
overrideprotecteddef onStart(): Unit = {
println("on start invoke")
}
}
FIFO队列的调用
package thread
/**
* Created by tianjun on 2017/8/22.
*/objectBootstrap {def main(args: Array[String]) {
val eventLoop = new TaskProcessEventLoop("task-event-loop")
eventLoop.start()
for(i<- 1 to 10){
eventLoop.post(TaskSubmitted(s"task-$i"))
}
Thread.sleep(10000)
}
}
运行结果
on start invoke
task-1task-2task-3task-4task-5task-6task-7task-8task-9task-10