EventLoop
来源:互联网 发布:工作软件英文怎么说 编辑:程序博客网 时间:2024/05/16 00:56
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dt.spark.sql
//package org.apache.spark.util
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.{BlockingQueue, LinkedBlockingDeque}
import scala.util.control.NonFatal
//import org.apache.spark.Logging
/**
* An event loop to receive events from the caller and process all events in the event thread. It
* will start an exclusive event thread to process all events.
*
* Note: The event queue will grow indefinitely. So subclasses should make sure `onReceive` can
* handle events in time to avoid the potential OOM.
*/
private[spark] class EventLoop[E](name: String) {
System.out.println("hello,we come")
protected def onError(e: Throwable): Unit = ???
protected def onReceive(event: E): Unit = ???
private val eventQueue: BlockingQueue[E] = new LinkedBlockingDeque[E]()
private val stopped = new AtomicBoolean(false)
private val eventThread = new Thread(name) {
setDaemon(true)
override def run(): Unit = {
try {
while (!stopped.get) {
val event = eventQueue.take()
try {
//onReceive(event)
System.out.println("ok")
} catch {
case NonFatal(e) => {
try {
// onError(e)
System.out.println("ok")
} catch {
case NonFatal(e) => System.out.println("Unexpected error in " + name, e)
}
}
}
}
} catch {
case ie: InterruptedException => // exit even if eventQueue is not empty
case NonFatal(e) => System.out.println("Unexpected error in " + name, e)
}
}
}
def start(): Unit = {
if (stopped.get) {
throw new IllegalStateException(name + " has already been stopped")
}
// Call onStart before starting the event thread to make sure it happens before onReceive
onStart()
eventThread.start()
}
def stop(): Unit = {
if (stopped.compareAndSet(false, true)) {
eventThread.interrupt()
var onStopCalled = false
try {
eventThread.join()
// Call onStop after the event thread exits to make sure onReceive happens before onStop
onStopCalled = true
onStop()
} catch {
case ie: InterruptedException =>
Thread.currentThread().interrupt()
if (!onStopCalled) {
// ie is thrown from `eventThread.join()`. Otherwise, we should not call `onStop` since
// it's already called.
onStop()
}
}
} else {
// Keep quiet to allow calling `stop` multiple times.
}
}
/**
* Put the event into the event queue. The event thread will process it later.
*/
def post(event: E): Unit = {
eventQueue.put(event)
}
/**
* Return if the event thread has already been started but not yet stopped.
*/
def isActive: Boolean = eventThread.isAlive
/**
* Invoked when `start()` is called but before the event thread starts.
*/
protected def onStart(): Unit = {}
/**
* Invoked when `stop()` is called and the event thread exits.
*/
protected def onStop(): Unit = {}
/**
* Invoked in the event thread when polling events from the event queue.
*
* Note: Should avoid calling blocking actions in `onReceive`, or the event thread will be blocked
* and cannot process events in time. If you want to call some blocking actions, run them in
* another thread.
*/
//protected def onReceive(event: E): Unit
/**
* Invoked if `onReceive` throws any non fatal error. Any non fatal error thrown from `onError`
* will be ignored.
*/
//protected def onError(e: Throwable): Unit
}
==================
//package org.apache.spark.util
package com.dt.spark.sql
object testEventLoop {
def main(args: Array[String]): Unit = {
//还要找时间 来搞 case event 回调
val dzhevent= new EventLoop("eventdzh")
dzhevent.start()
dzhevent.getClass()
System.out.println(dzhevent.toString())
System.out.println(dzhevent.getClass())
}
}
- EventLoop
- 带有TimerQueue的EventLoop
- EventLoop分析[1-4]
- 理解NodeJS的EventLoop
- netty5.0之EventLoop
- Reactor 模式 EventLoop类
- muduo::EventLoop分析
- Qt thread and eventloop
- Muduo:EventLoop“循环”什么?
- Netty 学习 - EventLoop
- netty eventLoop执行器
- EventLoop与EventLoopGroup
- Spark源码--EventLoop
- chapter18 EventLoop和EventLoopGroup
- muduo源码分析---EventLoop类
- squid源码分析1 ----EventLoop
- muduo : Reactor(EventLoop Poller Channel)
- 7 EventLoop and threading model
- Hdu 1007 Quoit Design【最近点对】
- 轻松学习之 IMP指针的作用
- android工具库
- HDOJ 1023 Train Problem II(卡特兰数+大数乘除法)
- Windows下使用git图形化工具提交代码到git上
- EventLoop
- 关于初学者上传文件到github的方法
- 如何设置Android的AVD模拟器可以输入中文
- Hibernate实体关系映射—Annotation
- 数据结构(7):树的存储结构
- CentOs下软件的安装
- 【JLOI2013】T1、T2、T3 Bzoj3190~3192
- 并查集初学——吸血鬼
- CCF-201512-2 消除类游戏(模拟)