This session was presented at the JFall 2011 conference in The Netherlands. It introduces the Fork/Join framework that was introduced in Java 7. Example code can be found at http://bit.y/jfall-forkjoin
What about threads?
‣ Heavyweight (try starCng a million)
‣ Implicit dependencies between tasks
‣ Manual synchronizaCon
‣ Deadlock/livelock/race condiCons
‣ Hard to scale to available parallelism
Map/Reduce?
Environment Single JVM Cluster
Model Recursive forking O^en single map
Scales with Cores/CPUs Nodes
Worker Workstealing No inter‐node
interacDon communicaCon
Fork/Join and
‣ ForkJoinPool starts threads
‣ Illegal in EJBs
‣ Fair game in servlets/CDI beans
‣ Don’t create ForkJoinPool for each request
‣ Idea: WorkManager to create poolthreads
‣ Single pool, async submit(ForkJoinTask)
‣ Don’t Ce up request thread: Servlet 3.0
Alternatives outside Java?
‣ Actors
‣ So^ware transacConal memory
‣ Dataflow concurrency
‣ Agents
‣ ... Some of this built on F/J
Akka GPars
Sander Mak, Info Support\nGaan het hebben over Doug Lea’s fork/join (al sinds 1998 in the works), nu onderdeel van Java7\nFun & Profit: profit vanwege snellere apps, fun hangt van je interesses af :)\n
No choice but to churn on each task sequentially\nPentium 5, 3.4Ghz -> hitting physical limits! (approach: speed, smarter instructions onchip)\nThreading alleviates IO contention, not CPU contention\n
No choice but to churn on each task sequentially\nPentium 5, 3.4Ghz -> hitting physical limits! (approach: speed, smarter instructions onchip)\nThreading alleviates IO contention, not CPU contention\n
More cores, lower speed. Do nothing -> your app could be slower! Embrace parallelism.\nIn webapps we get a lot of request-oriented parallelism for free.\nWhat about other applications/algorithms?\n
CPU architectures changed all the time without having to change Java code... why is this different? -> Implicit data-dependencies in code (also, shared memory/state problems)\n
Builds a tree with explicit dependencies between tasks!\nDivide-and-conquer algorithms\nHier: 6 fork acties, 6 join acties\n
Threshold is key: overhead vs. effective work per unit\nApplicable to for example:search, sort, aggregating data\n
ForkJoinTask is a future\n
- getAvailableProcessors on System.runtime\n- possible to set own size (e.g. restrict to 4 of n cores)\n
\n
\n
- forked tasks pushed on top local deque\n- idle worker steals from bottom deque of other worker -> prevent contention on deque, and largest tasks get stolen!\n- no work stolen? eventually yield workerthread. Typ. 0-2% stolen\n- Workstealing == loadbalancing without central coord!\n
Fibonacci in het begin bekeken. Allemaal reken-intensief.\nSprekend voorbeeld: password hashes tegen rainbow table aanhouden.\n
Still threads are better when doing blocking I/O networking etc.\n
\n
F/J: recursively apply same task, M/R: apply different map/reduce steps, not necessarily recursive (but could happen)\n
\n
Websphere staat bv ook geen threadcreatie in servlet toe\nEvt. JCA adapter noemen\n
\n
\n
\n
needs lambdas!\nbut already available in jsr166y with anonymous inner classes\n