spark(5)--spark模型中FIFO的实现
来源:互联网 发布:图片墙制作软件 编辑:程序博客网 时间:2024/06/08 18:42
pom文件的配置
<?xml version="1.0" encoding="UTF-8"?><project xmlns="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. */trait TaskEventcase class TaskSubmitted(name:String) extends TaskEventcase class TaskSucceeded(name:String) extends TaskEventcase class TaskFailed(name:String) extends TaskEvent
任务进队列,队列的处理的抽象类
package threadimport java.util.concurrent.atomic.AtomicBooleanimport java.util.concurrent.{LinkedBlockingDeque, BlockingQueue}import scala.util.control.NonFatal/** * Created by tianjun on 2017/8/22. */abstract class EventLoop[E](name: String) { private val eventQueue: BlockingQueue[E] = new LinkedBlockingDeque[E]() private val stopped = new AtomicBoolean(false) private val eventThread = new Thread(name){ override def 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 empty case NonFatal(e) => println("Unexpected error in "+name,e) } } } def start(): Unit = { if(stopped.get()){ throw new IllegalStateException(name+" has already been stopped") } onStart() eventThread.start() } def stop(): Unit ={ if(stopped.compareAndSet(false,true)){ eventThread.interrupt() var onStopCalled = false try{ 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 protected def onStart(): Unit = {} protected def onStop(): Unit = {} protected def onReceive: PartialFunction[E,Unit] protected def onError(e: Throwable): Unit}
队列处理的实现类
package thread/** * Created by tianjun on 2017/8/22. */class TaskProcessEventLoop(name:String) extends EventLoop[TaskEvent](name){ override protected def onReceive:PartialFunction[TaskEvent,Unit] = { case TaskSubmitted(taskName) => println(taskName) } override protected def onError(e:Throwable):Unit = { } override protected def onStart(): Unit = { println("on start invoke") }}
FIFO队列的调用
package thread/** * Created by tianjun on 2017/8/22. */object Bootstrap { 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 invoketask-1task-2task-3task-4task-5task-6task-7task-8task-9task-10
阅读全文
0 0
- spark(5)--spark模型中FIFO的实现
- Spark实现SVM模型
- 【Spark】RDD机制实现模型
- 基于spark mllib的LDA模型训练Scala代码实现
- Spark的应用程序执行模型
- spark下的贝叶斯模型
- spark 的实现简介
- Spark的shuffle实现
- spark中ml机器学习库的朴素贝叶斯模型实现中文文本信息的文类预测
- SPARK中实现自定义分区
- Spark调度模式-FIFO和FAIR
- [spark] 调度模式(FIFO&FAIR)
- 基于spark的新词发现模型
- Spark下的word2vec模型训练
- 利用spark的mllib构建GBDT模型
- Hadoop和Spark的处理模型比较
- Spark下的PageRank实现
- Spark自定义累加器的实现
- 数组中出现次数超过一半的数字
- hdu--6153(A Secret)
- [转]写给Krpano小白们的最最最入门级教程(二)
- 【容斥原理】Codeforces547C[Mike and Foam]题解
- 8.22最短路径讲解
- spark(5)--spark模型中FIFO的实现
- hdu 2896 ac自动机
- lintcode——买卖股票的最佳时机
- 2017.08.22 今日头条笔试题
- MyBatis(1)--MyBatis入门示例
- Buggy Robot Gym
- Guava Cache源码解析
- 智能停车场解决方案
- 按钮组件