Loading presentation...
Prezi is an interactive zooming presentation

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Concurrency Options on the JVM

StrangeLoop, September 18, 2014
by

Jessica Kerr

on 18 September 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Concurrency Options on the JVM

Concurrency Options on the JVM
Shared State
actors
channels
parallel
streams

futures
ExecutorService
Executors.
newSingleThreadExecutor()
System.exit(0)
es.shutdown()
keepAliveTime
daemon
thread.setDaemon(true);
thread = Executors.defaultThreadFactory.newThread()
return thread;
public Thread newThread() {
}
new ThreadFactory {
}
Executors.newSingleThreadExecutor(
);
es.shutdown();
es.awaitTermination(5, TimeUnit.SECONDS);
explicit shutdown
Scala akka
clojure
send
send-off
explicit shutdown
1-min timeout
daemon threads
clojure core.async
daemon threads
Java
Scala
Java
Scala
Clojure
Scalaz
void execute(Runnable)
Future submit(Callable)
CompletableFuture
.supplyAsync(Supplier, Executor)
ForkJoinPool.commonPool()
daemon threads
fjp.awaitQuiescence(4, MINUTES)
Future(stuffToDo)(implicit ExecutionContext)
ExecutionContext.global
ForkJoinPool
daemon threads
myFuture.map
{ old =>
becomeNew
}
(anotherExecutionContext)
(future stuff-to-do)
agent-send-off-executor
keepAliveTime = 1 min
infinite threads
Future
Task
Task(stuffToDo)(implicit ExecutorService = default)
FixedThreadPool
but daemon threads
N = available processors
Single
Thread
Executor
liveness
owning mutable state
Fixed
Thread
Pool
Do Not Block
throttling
no recursive blocking
watch out for other people's code.
Cached
Thread
Pool
blocking is OK
unlimited threads
keepAlive = 1 min
unlimited queue
explicit shutdown
unbounded queue
explicit shutdown
no queueing
direct handoff of new tasks to threads
SynchronousQueue
Fork
Join
Pool
tasks that spawn more!
maximize processor power
declare blocking operations
daemon threads
unbounded queues
clever performance tricks
jstack
thread.setName(
"my-pool-"
+
counter.getAndIncrement()
);
thread = Executors.defaultThreadFactory.newThread()
return thread;
public Thread newThread() {
}
new ThreadFactory {
}
Executors.newCachedThreadExecutor(
);
AtomicLong
counter
=
new AtomicLong(0);
thread.
setUncaughtExceptionHandler
(?);
myFuture.onFailure
{
case t => t.printStackTrace()
}
(myExecutionContext)
CachedThreadPool
agents
pool-induced
deadlock
capture
exceptions
ForkJoinPool
throw exceptions
FixedThreadPool(2*N+42)
channel death
FixedThreadPool(N+2)
CachedThreadPool
error-handler
agent-error
ForkJoinPool
How to Concurrency:
a. Don't do it
b. Don't do it yourself
c. Ask yourself:
Whose threads am I using?
How long is too long?
ThreadPool
+
BlockingQueue
Executor
ExecutorService
ThreadPoolExecutor
Executors
creates
implements
scala.concurrent.
blocking
{
service.callinate()
}
errors handled by Supervisor hierarchy
1. What is "done"?
2. How many is the right many?
3. What happens when
failure
?

Thread
Pool
Executor
core pool size
max pool size
queue
prestart core threads
thread factory
keep-alive time
SynchronousQueue
LinkedBlockingQueue
ArrayBlockingQueue
0

n
PriorityBlockingQueue

rejected execution handler
RejectedExecutionException
Caller runs
Discard task
Discard oldest task
beforeExecute
afterExecute
allowCoreThreadTimeout
thread group
priority
daemon
name
uncaught exception handler
context class loader
Thread
2 stacks - 1M memory
1 program counter
native resource
1 flow of execution
trampolining
Full transcript