Created
October 20, 2009 01:36
-
-
Save dcbriccetti/213914 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox | |
import actors.Actor | |
import org.apache.log4j.Logger | |
object getter extends Actor { | |
val logger = Logger.getLogger("Actor") | |
def act() { | |
while (true) { | |
receive { | |
case msg => | |
logger.info("received message: "+ msg) | |
Thread.sleep(1000) // Simulate long-running operation | |
} | |
} | |
} | |
} | |
object Main { | |
def main(args: Array[String]): Unit = { | |
getter.start() | |
getter ! "hi" | |
getter ! "hi" | |
getter ! "hi" | |
Thread.sleep(4000) | |
} | |
} | |
/* | |
18:34:14,539 [Thread-1] INFO Actor - received message: hi | |
18:34:15,542 [Thread-1] INFO Actor - received message: hi | |
18:34:16,543 [Thread-1] INFO Actor - received message: hi | |
I want all three “long-running tasks” to run concurrently (probably in separate Java threads). | |
*/ | |
/* | |
I'd like the equivalent of this, actually: | |
*/ | |
package org.talkingpuffin.util | |
import java.util.concurrent.{Callable, Executors} | |
object Parallelizer { | |
/** | |
* Runs, in the number of threads requested, the function f, giving it each A of args, returning a List[T] | |
*/ | |
def run[T,A,F](numThreads: Int, args: List[A], f: (A) => T): List[T] = { | |
val pool = Executors.newFixedThreadPool(numThreads) | |
(for { | |
arg <- args | |
future = pool.submit(new Callable[T] {def call = f(arg)}) | |
} yield future).map(_.get) | |
} | |
} | |
// ----- | |
def getLists: List[TwitterLists] = { | |
for { | |
twitterLists <- Parallelizer.run(20, screenNames, session.twitterSession.getLists) | |
if twitterLists.isDefined | |
} yield twitterLists.get | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment