本文整理汇总了Scala中java.util.concurrent.Executors类的典型用法代码示例。如果您正苦于以下问题:Scala Executors类的具体用法?Scala Executors怎么用?Scala Executors使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Executors类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Server
//设置package包名称以及导入依赖的类
package sh.webserver
import java.net.ServerSocket
import java.util.concurrent.{ExecutorService, Executors}
import sh.webserver.request.Request
import scala.annotation.tailrec
class Server(port: Int) {
def start() {
val server = new ServerSocket(port)
val pool = Executors.newFixedThreadPool(8)
listen(server, pool)
}
@tailrec
private def listen(server : ServerSocket,pool : ExecutorService) {
val socket = server.accept()
pool.execute(new RequestHandler(socket))
listen(server, pool)
}
}
示例2: KafkaClient
//设置package包名称以及导入依赖的类
package services.Kafka
import java.util.Properties
import java.util.concurrent.Executors
import akka.actor.{Props, DeadLetter, ActorSystem}
import kafka.consumer.{Consumer, ConsumerConfig}
import scala.concurrent.{ExecutionContext, Future}
object KafkaClient {
val config = {
val properties = new Properties()
properties.put("bootstrap.servers", "localhost:9092")
properties.put("group.id", "pogo_consumer")
properties.put("auto.offset.reset", "largest")
properties.put("zookeeper.connect", "localhost:2181")
properties.put("zookeeper.session.timeout.ms", "400")
properties.put("zookeeper.sync.time.ms", "200")
properties.put("auto.commit.interval.ms", "500")
new ConsumerConfig(properties)
}
// Our actor system managing our actors
val system = ActorSystem("es-sharpshooter")
// Taking care of dead letters
system.eventStream.subscribe(system.actorOf(Props[IndexService], "dead-letters"), classOf[DeadLetter])
// Dedicated Kafka Execution context
implicit val KafkaContext = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(20))
def start = {
// Starting our consumer
val consumer = Consumer.create(config)
val topics = Map(
"pokemons" -> 1,
"spawnpoints" -> 1
)
val streams = consumer.createMessageStreams(topics)
// Start the consumer asynchronously
Future {
streams.get("pokemons").get.foreach(PokemonService.cycle(system))
} onFailure { case ec => println(ec) }
Future {
streams.get("spawnpoints").get.foreach(SpawnService.cycle(system))
} onFailure { case ec => println(ec) }
}
}
示例3: Server
//设置package包名称以及导入依赖的类
package com.scala.examples.datagurn.lesson08
import com.scala.examples.datagurn.lesson07.Teacher
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import scala.concurrent.impl.Future
import java.util.concurrent.FutureTask
import scala.concurrent.impl.Future
import com.scala.examples.datagurn.lesson07.Teacher
object Server {
// ????????????
val executorService: ExecutorService = Executors.newFixedThreadPool(10);
val teachers: List[Teacher] = List(
new Teacher("t1", 20),
new Teacher("t2", 30),
new Teacher("t1", 25),
new Teacher("t1", 21)
);
def concurrentFilterByName(name: String): FutureTask[List[Teacher]] = {
val futures = new FutureTask[List[Teacher]](new Callable[List[Teacher]]() {
def call(): List[Teacher] = {
return filterByName(name);
}
})
executorService.execute(futures);
return futures; // ?? Future
}
def filterByName(name: String) : List[Teacher] = {
teachers.synchronized {
for {
item <- teachers
if item.name == name
} yield item
}
}
def main(args: Array[String]): Unit = {
val teachers : List[Teacher] = concurrentFilterByName("t1").get
println(teachers)
executorService.shutdown();
}
}
示例4: RemoteConnection
//设置package包名称以及导入依赖的类
package akka.remote.testconductor
import org.jboss.netty.channel.{ Channel, ChannelPipeline, ChannelPipelineFactory, ChannelUpstreamHandler, SimpleChannelUpstreamHandler, DefaultChannelPipeline }
import org.jboss.netty.channel.socket.nio.{ NioClientSocketChannelFactory, NioServerSocketChannelFactory }
import org.jboss.netty.bootstrap.{ ClientBootstrap, ServerBootstrap }
import org.jboss.netty.handler.codec.frame.{ LengthFieldBasedFrameDecoder, LengthFieldPrepender }
import org.jboss.netty.handler.codec.compression.{ ZlibDecoder, ZlibEncoder }
import org.jboss.netty.handler.codec.protobuf.{ ProtobufDecoder, ProtobufEncoder }
import org.jboss.netty.handler.timeout.{ ReadTimeoutHandler, ReadTimeoutException }
import java.net.InetSocketAddress
import java.util.concurrent.Executors
import akka.event.Logging
import akka.util.Helpers
private[akka] object RemoteConnection {
def apply(role: Role, sockaddr: InetSocketAddress, poolSize: Int, handler: ChannelUpstreamHandler): Channel = {
role match {
case Client ?
val socketfactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool, Executors.newCachedThreadPool,
poolSize)
val bootstrap = new ClientBootstrap(socketfactory)
bootstrap.setPipelineFactory(new TestConductorPipelineFactory(handler))
bootstrap.setOption("tcpNoDelay", true)
bootstrap.connect(sockaddr).getChannel
case Server ?
val socketfactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool, Executors.newCachedThreadPool,
poolSize)
val bootstrap = new ServerBootstrap(socketfactory)
bootstrap.setPipelineFactory(new TestConductorPipelineFactory(handler))
bootstrap.setOption("reuseAddress", !Helpers.isWindows)
bootstrap.setOption("child.tcpNoDelay", true)
bootstrap.bind(sockaddr)
}
}
def getAddrString(channel: Channel) = channel.getRemoteAddress match {
case i: InetSocketAddress ? i.toString
case _ ? "[unknown]"
}
def shutdown(channel: Channel) =
try channel.close() finally try channel.getFactory.shutdown() finally channel.getFactory.releaseExternalResources()
}
示例5: UserCache
//设置package包名称以及导入依赖的类
package com.init6.db
import java.util.concurrent.{Executors, TimeUnit}
import com.init6.Config
import com.init6.utils.CaseInsensitiveHashMap
import scala.collection.mutable
import scala.concurrent.duration.Duration
import scala.util.Try
private[db] class UserCache(dbUsers: List[DbUser]) {
private val cache = CaseInsensitiveHashMap[DbUser]()
private val inserted = mutable.HashSet[String]()
private val updated = mutable.HashSet[String]()
private val executorService = Executors.newSingleThreadScheduledExecutor()
private val updateInterval = Config().Database.batchUpdateInterval
private val dbUpdateThread = new Runnable {
override def run() = {
Try {
DAO.saveInserted(cache.filterKeys(inserted.contains).values.toSet)
inserted.clear()
}
Try {
DAO.saveUpdated(cache.filterKeys(updated.contains).values.toSet)
updated.clear()
}
}
}
cache ++= dbUsers.map(dbUser => dbUser.username -> dbUser)
executorService.scheduleWithFixedDelay(dbUpdateThread, updateInterval, updateInterval, TimeUnit.SECONDS)
def close() = {
executorService.shutdown()
dbUpdateThread.run()
}
def get(username: String) = cache.get(username)
def insert(username: String, password_hash: Array[Byte]) = {
val now = System.currentTimeMillis
val newUser = username.toLowerCase
cache += newUser -> DbUser(username = newUser, password_hash = password_hash,
created = now, last_logged_in = now)
inserted += username.toLowerCase
}
def update(username: String, dbUser: DbUser) = {
get(username).foreach(originalDbUser => {
if (originalDbUser != dbUser) {
cache += username -> dbUser
updated += username.toLowerCase
}
})
}
}
示例6: NettyBench
//设置package包名称以及导入依赖的类
package com.example
import java.time.Duration
import java.time.Instant
import java.util.concurrent.Executors
import com.naoh.beef.Client
import com.naoh.beef.Server
import com.naoh.beef.proto.echo.EchoGrpc
import com.naoh.beef.proto.echo.EchoGrpc.EchoBlockingStub
import com.naoh.beef.proto.echo.EchoReq
import io.grpc.CallOptions
import io.grpc.ManagedChannelBuilder
import io.grpc.ServerBuilder
import io.grpc.netty.NettyChannelBuilder
import io.grpc.netty.NettyServerBuilder
import scala.concurrent.ExecutionContext
import scala.util.Try
class NettyBench {
val serverCtx = ExecutionContext.fromExecutorService(Executors.newScheduledThreadPool(8))
val clientCtx = ExecutionContext.fromExecutorService(Executors.newScheduledThreadPool(8))
val server = NettyServerBuilder.forPort(8899).addService(EchoGrpc.bindService(EchoImpl, serverCtx)).build().start()
val ch = NettyChannelBuilder.forAddress("localhost", 8899).usePlaintext(true).build()
val client = new EchoBlockingStub(ch, CallOptions.DEFAULT.withExecutor(clientCtx))
Thread.sleep(1000)
val base = Instant.now
Iterator.range(0, 3000).toSeq.toParArray.foreach { _ => Try(client.retEcho(EchoReq("12"))); print(".") }
val record = Duration.between(base, Instant.now())
println(s"\n\nDuration $record \n")
Thread.sleep(2000)
clientCtx.shutdown()
serverCtx.shutdown()
server.shutdown()
ch.shutdown()
}
示例7: BeefTest
//设置package包名称以及导入依赖的类
package com.example
import java.time.Duration
import java.time.Instant
import java.util.concurrent.Executors
import akka.actor.ActorSystem
import akka.cluster.Cluster
import com.naoh.beef.Auth
import com.naoh.beef.Beef
import com.naoh.beef.Client
import com.naoh.beef.Region
import com.naoh.beef.Server
import com.naoh.beef.proto.echo.EchoGrpc
import com.naoh.beef.proto.echo.EchoReq
import com.typesafe.config.ConfigFactory
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.util.Try
class BeefTest {
val serverSystem = ActorSystem("MyActorSystem", ConfigFactory.parseResources("server.conf").resolve())
val clientSystem = ActorSystem("MyActorSystem", ConfigFactory.parseResources("client.conf").resolve())
Cluster(serverSystem).join(Cluster(serverSystem).selfAddress)
Cluster(clientSystem).join(Cluster(serverSystem).selfAddress)
val serverCtx = ExecutionContext.fromExecutorService(Executors.newScheduledThreadPool(8))
val clientCtx = ExecutionContext.fromExecutorService(Executors.newScheduledThreadPool(8))
val region = Region("rg")
val auth = Auth("au")
Thread.sleep(1000)
Beef(serverSystem)(
Server(region)
<< EchoGrpc.bindService(EchoImpl, serverCtx))
Thread.sleep(1000)
val builder = Client(region, auth, clientCtx) connect Beef(clientSystem)
val client = builder.build(new EchoGrpc.EchoBlockingStub(_, _))
Thread.sleep(1000)
val base = Instant.now
Iterator.range(0, 3000).toSeq.toParArray.foreach{_ => Try(client.retEcho(EchoReq("12"))); print(".")}
val record = Duration.between(base, Instant.now())
println(s"\n\nDuration $record \n")
Thread.sleep(2000)
clientCtx.shutdown()
serverCtx.shutdown()
clientSystem.shutdown()
serverSystem.shutdown()
}
示例8: Server
//设置package包名称以及导入依赖的类
package pubsub
import java.net.ServerSocket
import java.net.Socket
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.URL
import java.util.concurrent.Executors
import scala.concurrent.JavaConversions._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import pubsub.collection._
import pubsub.command._
import pubsub.network.TCPReader
object Server extends App {
val port = 7676
val maxWorkers = 12
val bufferSize = 20
val socket = new ServerSocket(port)
try {
val whatismyip = new URL("http://checkip.amazonaws.com")
val in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
val serverIP = in.readLine()
println(s"Connect to $serverIP (or `localhost`), port $port with `telnet` to join this server")
} catch {
case e: Exception =>
println("There is a problem with your internet connection, you can only access it via localhost")
}
val buffer = new BoundedBuffer[Command](20)
val commandHandlers = for{
i <- 0 until maxWorkers
} yield {
Future {
new CommandHandler(buffer).handle()
}
}
val threadPool = Executors.newFixedThreadPool(maxWorkers)
var clientId = 0
while(true) {
val client = socket.accept();
val cid = clientId
clientId += 1
Future{
new TCPReader(clientId, client, buffer).read()
}(threadPool)
}
}
示例9: HttpSecondariesExecutor
//设置package包名称以及导入依赖的类
package com.uglylabs.httpduplicator
import com.typesafe.scalalogging.Logger
import Utils._
import org.apache.http.util.EntityUtils
class HttpSecondariesExecutor(poolSize: Int, config: ServerConfig) {
private val log = Logger[HttpSecondariesExecutor]
private val client = createHttpClient(config)
import java.util.concurrent.Executors
import scala.concurrent._
implicit private val ec = new ExecutionContext {
val threadPool = Executors.newFixedThreadPool(poolSize)
def execute(runnable: Runnable) =
threadPool.submit(runnable)
def reportFailure(t: Throwable) =
log.error("Thread pool error", t)
}
def enqueue(target: HttpTarget, request: HttpRequest) {
Future {
log.debug("Send request to secondary target: {}\n{}", target, formatRequest(request))
val response = timing(s"Processing time for secondary $target : %dms", log.debug(_)) {
client.execute(target.toHttpHost, request)
}
log.debug("Receive response from secondary target: {}\n{}", target, formatResponse(response))
// release connection resources allocated to receive entity content
EntityUtils.consume(response.getEntity())
}.onFailure {
case e => log.error(s"Error serve request to target: $target", e)
}
}
}
示例10: CSVWriteBufOwnParallelTest
//设置package包名称以及导入依赖的类
import java.io.{BufferedWriter, FileWriter, PrintWriter}
import java.util.concurrent.Executors
import org.scalatest._
import scala.concurrent.{ExecutionContext, Await, Future}
import scala.concurrent.duration.Duration
import scala.util.{Success, Failure}
class CSVWriteBufOwnParallelTest extends FlatSpec with Matchers {
"Hello" should "have tests" in {
lazy val doit: Int => Unit = i => {
val bw = new PrintWriter(new BufferedWriter(new FileWriter(s"""/tmp/csv${i}.txt"""), 8192 * 256 * 4))
(1 to 10000000).foreach(i => {
bw.print(List(1, 2, 3, 4, 5, 6).mkString(","))
bw.print("\r\n")
})
bw.close()
}
def timeSpentDoing(f: => Unit) = {
val start = System.currentTimeMillis
println(start)
f
System.currentTimeMillis - start
}
implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(5))
val futures = (9 to 12).map(n => Future{ timeSpentDoing( doit(n) ) })
val combined = Future.sequence(futures)
Await.ready(combined, Duration.Inf)
combined onComplete {
case Success(duration) => println(duration)
case Failure(ex) => println(ex)
}
}
}
示例11: BankAccountEventHandler
//设置package包名称以及导入依赖的类
package org.styx.mongo
import java.util.concurrent.Executors
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.styx.bank.example.events._
import org.styx.bank.example.state.BankAccount
import org.styx.handler.{EventFetcher, EventHandler}
import org.styx.model.Event
import scala.concurrent.ExecutionContext
object BankAccountEventHandler {
implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(30))
val mapper = new ObjectMapper
mapper.registerModule(DefaultScalaModule)
def converter: MongoDBEvent => Event[BankAccount] = { mongoDBEvent =>
val event = mongoDBEvent match {
case MongoDBEvent("BankAccountCreated", eventDate, version, _, _) => BankAccountCreated(version, eventDate)
case MongoDBEvent("BankAccountClosed", eventDate, version, _, _) => BankAccountClosed(version, eventDate)
case MongoDBEvent("OwnerChanged", eventDate, version, _, _) => OwnerChanged(version, eventDate)
case MongoDBEvent("DepositPerformed", eventDate, version, _, _) => DepositPerformed(version, eventDate)
case MongoDBEvent("WithdrawalPerformed", eventDate, version, _, _) => WithdrawalPerformed(version, eventDate)
}
event.data = mapper.readValue(mongoDBEvent.data.toJson(), classOf[Map[String, Any]])
event
}
implicit val eventHandler: EventHandler[BankAccount] with EventFetcher[BankAccount] = MongoDBEventHandlerFetcher(MongoD.collection, mapper, converter)
}
示例12: MessageRelayManager
//设置package包名称以及导入依赖的类
package io.grhodes.mcm.server.gcm
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.{DelayQueue, Executors}
import com.gilt.gfc.logging.Loggable
import scala.util.control.NonFatal
class MessageRelayManager(val gcmMessageStore: GCMMessageStore) extends Loggable {
private val executorService = Executors.newFixedThreadPool(50)
private val queue = new DelayQueue[ScheduledJob]()
private val keepRunning = new AtomicBoolean(true)
private val schedulerFuture = executorService.submit(new JobScheduler(this))
def shutdown(): Boolean = {
this.keepRunning.set(false)
gcmMessageStore.shutdown()
queue.clear()
schedulerFuture.cancel(true)
true
}
def addJob(job: ScheduledJob): Boolean = queue.add(job)
class JobScheduler(relayManager: MessageRelayManager) extends Runnable {
override def run(): Unit = {
while (keepRunning.get()) {
try {
val scheduledJob = queue.take()
executorService.execute(new Runnable() {
override def run(): Unit = scheduledJob.execute(relayManager)
});
} catch {
case NonFatal(e) =>
error("JobScheduler.run() got exception:", e)
}
}
}
}
}
示例13: ExchangeSpec
//设置package包名称以及导入依赖的类
package ru.tolsi.matcher.naive
import java.util.concurrent.Executors
import scala.concurrent.ExecutionContext
import ru.tolsi.matcher.{AbstractExchangeSpec, ClientInfo, UnitSpec}
class ExchangeSpec extends UnitSpec
with AbstractExchangeSpec[ThreadUnsafeClientRepository, SingleThreadOrderBook, SingleThreadOrderExecutor] {
override def ec: ExecutionContext = ExecutionContext.fromExecutor(Executors.newSingleThreadExecutor())
override def buildClientRepo(clients: Seq[ClientInfo]): ThreadUnsafeClientRepository = ThreadUnsafeClientRepository(
clients.map(ThreadUnsafeClient.fromClientInfo))
override def orderBook: SingleThreadOrderBook = new SingleThreadOrderBook
override def ordersExecutor: SingleThreadOrderExecutor = new SingleThreadOrderExecutor
override def extraCheck(
repo: ThreadUnsafeClientRepository,
orderBook: SingleThreadOrderBook,
executor: SingleThreadOrderExecutor): Unit = {
forAll(orderBook.instrumentsOrderBook.values.flatMap(_.values.map(_.isEmpty))) { isEmpty =>
isEmpty should be(true)
}
}
}
示例14: SwaveIdentityProcessorVerification
//设置package包名称以及导入依赖的类
package swave.core.tck
import java.util.concurrent.{ExecutorService, Executors, TimeUnit}
import org.reactivestreams.Publisher
import org.reactivestreams.tck.{IdentityProcessorVerification, TestEnvironment}
import org.scalatest.testng.TestNGSuiteLike
import org.testng.SkipException
import org.testng.annotations.AfterClass
import swave.core._
abstract class SwaveIdentityProcessorVerification[T](val testEnv: TestEnvironment, publisherShutdownTimeout: Long)
extends IdentityProcessorVerification[T](testEnv, publisherShutdownTimeout) with TestNGSuiteLike
with StreamEnvShutdown {
def this(printlnDebug: Boolean) =
this(
new TestEnvironment(Timeouts.defaultTimeout.toMillis, printlnDebug),
Timeouts.publisherShutdownTimeout.toMillis)
def this() = this(false)
override def createFailedPublisher(): Publisher[T] =
Spout.failing[T](new Exception("Nope")).drainTo(Drain.toPublisher()).get
// Publishers created by swave don't support fanout by default
override def maxSupportedSubscribers: Long = 1L
override def required_spec313_cancelMustMakeThePublisherEventuallyDropAllReferencesToTheSubscriber(): Unit =
throw new SkipException("Not relevant for publisher w/o fanout support")
override lazy val publisherExecutorService: ExecutorService =
Executors.newFixedThreadPool(3)
@AfterClass
def shutdownPublisherExecutorService(): Unit = {
publisherExecutorService.shutdown()
publisherExecutorService.awaitTermination(3, TimeUnit.SECONDS)
}
}
示例15: PooledContexts
//设置package包名称以及导入依赖的类
package support
import java.util.concurrent.{ExecutorService, Executors, TimeUnit}
import scala.concurrent.ExecutionContext
object PooledContexts extends LoggerSupport {
private def newService(threadCount:Int):ExecutorService =
ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(threadCount))
private def ctx(service:ExecutorService) = ExecutionContext.fromExecutor(service)
private val db = newService(10)
implicit val dbContext:ExecutionContext = ctx(db)
private val app = newService(5)
implicit val appContext:ExecutionContext = ctx(app)
def shutdown(): Unit = {
Seq(db, app).foreach {svc =>
try {
svc.awaitTermination(Long.MaxValue, TimeUnit.NANOSECONDS)
} catch {
case ex:Exception => Logger.warn("termination failed", ex)
} finally {
svc.shutdownNow()
}
}
}
}