当前位置: 首页>>代码示例>>Scala>>正文


Scala concurrent类代码示例

本文整理汇总了Scala中scala.collection.concurrent的典型用法代码示例。如果您正苦于以下问题:Scala concurrent类的具体用法?Scala concurrent怎么用?Scala concurrent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了concurrent类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: ResourceCache

//设置package包名称以及导入依赖的类
package se.gigurra.dcs.remote

import java.time.Instant

import com.google.common.cache.CacheBuilder
import com.google.common.cache.Cache
import com.twitter.io.Buf

import scala.collection.concurrent
import scala.collection.JavaConversions._

case class ResourceCache(maxItemsPerCategory: Int) {

  private val categories = new concurrent.TrieMap[String, Cache[String, CacheItem]]

  def put(category: String, id: String, _data: Buf): Unit = {
    val data = Buf.ByteArray.coerce(_data)
    categories.getOrElseUpdate(category, newCategory()).put(id, CacheItem(id, data, time))
  }

  def get(category: String, id: String, maxAgeSeconds: Double): Option[CacheItem] = {
    categories.get(category).flatMap(c => Option(c.getIfPresent(id))).filter(_.age <= maxAgeSeconds)
  }

  def delete(category: String, id: String): Unit = {
    categories.get(category).foreach(_.invalidate(id))
  }

  def getCategory(category: String, maxAgeSeconds: Double): Seq[CacheItem] = {
    categories.get(category).map { c =>
      c.asMap().values().filter(_.age <= maxAgeSeconds).toSeq
    }.getOrElse(Nil)
  }

  def categoryNames: Seq[String] = {
    categories.keys.toSeq
  }

  private def time: Double = Instant.now.toEpochMilli.toDouble / 1000.0
  private def newCategory() = CacheBuilder.newBuilder().maximumSize(maxItemsPerCategory).build[String, CacheItem]()
}

case class CacheItem(id: String, data: Buf, timestamp: Double) {
  def age: Double = time - timestamp
  private def time: Double = Instant.now.toEpochMilli.toDouble / 1000.0
} 
开发者ID:GiGurra,项目名称:dcs-remote2,代码行数:47,代码来源:ResourceCache.scala

示例2: createNewExecutionContext

//设置package包名称以及导入依赖的类
package com.smule.smg

import java.util.concurrent.{Executors, ConcurrentHashMap}

import scala.collection.concurrent
import scala.collection.JavaConversions._

import play.libs.Akka

import scala.concurrent.ExecutionContext


  val rrdGraphCtx: ExecutionContext = Akka.system.dispatchers.lookup("akka-contexts.rrd-graph")

  val monitorCtx: ExecutionContext = Akka.system.dispatchers.lookup("akka-contexts.monitor")

  private val log = SMGLogger

  private val ctxMap: concurrent.Map[Int,ExecutionContext] = new ConcurrentHashMap[Int, ExecutionContext]()

  private def createNewExecutionContext(maxThreads: Int): ExecutionContext = {
    val es = Executors.newFixedThreadPool(maxThreads)
    ExecutionContext.fromExecutorService(es)
  }

  def ctxForInterval(interval: Int): ExecutionContext = {
    ctxMap(interval)
  }

  def initializeUpdateContexts(intervals: Seq[Int], threadsPerIntervalMap: Map[Int,Int], defaultThreadsPerInterval: Int): Unit = {
    intervals.foreach { interval =>
      if (!ctxMap.contains(interval)) {
        val maxThreads = if (threadsPerIntervalMap.contains(interval)) threadsPerIntervalMap(interval) else defaultThreadsPerInterval
        val ec = createNewExecutionContext(maxThreads)
        ctxMap(interval) = ec
        log.info("ExecutionContexts.initializeUpdateContexts: Created ExecutionContext for interval=" + interval +
          " maxThreads=" + maxThreads + " ec.class="+ ec.getClass.getName)
      }
    }
  }
} 
开发者ID:asen,项目名称:smg,代码行数:42,代码来源:ExecutionContexts.scala

示例3: WeiScheduling

//设置package包名称以及导入依赖的类
package com.weibo.datasys.job.mesos

import com.nokia.mesos.api.async.Scheduling
import com.nokia.mesos.api.async.TaskLauncher._
import com.nokia.mesos.impl.launcher.WeiTaskAllocator
import org.apache.mesos.mesos.{ Offer, OfferID }

import scala.collection.concurrent
import scala.concurrent.Future


class WeiScheduling extends Scheduling with WeiTaskAllocator {

  private[this] val currentOffers = new concurrent.TrieMap[OfferID, Offer]

  def offer(offers: Seq[Offer]): Unit = {
    currentOffers ++= offers.map(o => (o.id, o))
  }

  def schedule(tasks: Seq[TaskRequest], filter: Filter, urgency: Float): Future[TaskAllocation] = {
    val optAllocation: Option[TaskAllocation] = tryAllocate(
      currentOffers.values.toSeq,
      tasks,
      if (filter eq NoFilter) None else Some(filter)
    )
    optAllocation.fold {
      Future.failed[TaskAllocation](Scheduling.NoMatch())
    } { allocation =>
      // we cannot use these offers any more:
      this.rescind(allocation
        .filter { case (off, tsks) => tsks.nonEmpty }
        .map(_._1.id)
        .toSeq)

      Future.successful(allocation)
    }
  }

  def rescind(offers: Seq[OfferID]): Unit = {
    for (off <- offers) {
      currentOffers.remove(off)
    }
  }
} 
开发者ID:batizty,项目名称:wolong,代码行数:45,代码来源:WeiScheduling.scala

示例4: InputStreamingChannelInitializer

//设置package包名称以及导入依赖的类
package com.bwsw.sj.engine.input.connection.tcp.server

import java.util.concurrent.ArrayBlockingQueue

import com.bwsw.sj.engine.core.input.InputStreamingExecutor
import io.netty.buffer.ByteBuf
import io.netty.channel.socket.SocketChannel
import io.netty.channel.{ChannelHandlerContext, ChannelInitializer}
import io.netty.handler.codec.string.StringEncoder
import io.netty.handler.logging.{LogLevel, LoggingHandler}

import scala.collection.concurrent


class InputStreamingChannelInitializer(executor: InputStreamingExecutor,
                                       channelContextQueue: ArrayBlockingQueue[ChannelHandlerContext],
                                       bufferForEachContext: concurrent.Map[ChannelHandlerContext, ByteBuf])
  extends ChannelInitializer[SocketChannel] {

  def initChannel(channel: SocketChannel) = {
    val pipeline = channel.pipeline()

    pipeline.addLast("logger", new LoggingHandler(LogLevel.WARN))
    pipeline.addLast("encoder", new StringEncoder())
    pipeline.addLast("handler", new InputStreamingServerHandler(executor, channelContextQueue, bufferForEachContext))
  }
} 
开发者ID:bwsw,项目名称:sj-platform,代码行数:28,代码来源:InputStreamingChannelInitializer.scala

示例5: InputStreamingServer

//设置package包名称以及导入依赖的类
package com.bwsw.sj.engine.input.connection.tcp.server

import java.util.concurrent.{Callable, ArrayBlockingQueue}

import com.bwsw.sj.engine.core.input.InputStreamingExecutor
import io.netty.bootstrap.ServerBootstrap
import io.netty.buffer.ByteBuf
import io.netty.channel._
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.handler.logging.{LogLevel, LoggingHandler}
import org.slf4j.LoggerFactory

import scala.collection.concurrent



class InputStreamingServer(host: String,
                           port: Int,
                           executor: InputStreamingExecutor,
                           channelContextQueue: ArrayBlockingQueue[ChannelHandlerContext],
                           bufferForEachContext: concurrent.Map[ChannelHandlerContext, ByteBuf]) extends Callable[Unit] {

  private val logger = LoggerFactory.getLogger(this.getClass)

  override def call() = {
    logger.info(s"Launch input streaming server on: '$host:$port'\n")
    val bossGroup: EventLoopGroup = new NioEventLoopGroup()
    val workerGroup = new NioEventLoopGroup()
    try {
      val bootstrapServer = new ServerBootstrap()
      bootstrapServer.group(bossGroup, workerGroup)
        .channel(classOf[NioServerSocketChannel])
        .handler(new LoggingHandler(LogLevel.INFO))
        .childHandler(new InputStreamingChannelInitializer(executor,  channelContextQueue, bufferForEachContext))

      bootstrapServer.bind(host, port).sync().channel().closeFuture().sync()
    } finally {
      workerGroup.shutdownGracefully()
      bossGroup.shutdownGracefully()
    }
  }
} 
开发者ID:bwsw,项目名称:sj-platform,代码行数:44,代码来源:InputStreamingServer.scala

示例6: InMemoryOffsetStore

//设置package包名称以及导入依赖的类
package com.lightbend.lagom.spi.persistence

import akka.Done
import akka.persistence.query.{ NoOffset, Offset }

import scala.concurrent.Future
import scala.collection.concurrent


class InMemoryOffsetStore extends OffsetStore {
  private final val store: concurrent.Map[String, Offset] = concurrent.TrieMap.empty

  override def prepare(eventProcessorId: String, tag: String): Future[OffsetDao] = {
    val key = s"$eventProcessorId-$tag"
    Future.successful(new OffsetDao {
      override def saveOffset(offset: Offset): Future[Done] = {
        store.put(key, offset)
        Future.successful(Done)
      }
      override val loadedOffset: Offset = store.getOrElse(key, NoOffset)
    })
  }
} 
开发者ID:lagom,项目名称:lagom,代码行数:24,代码来源:OffsetStore.scala


注:本文中的scala.collection.concurrent类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。