SlideShare a Scribd company logo
Clojure concurrency overview
Agenda

-Overview
-Clojure features
-Concurrent programming
-Vars
-Refs and STM approach
-Atoms
-Agents
Overview

Rich Hickey
https://twitter.com/#!/richhickey




-about 2.5 years working on Clojure
-first public release at 2007
-interview: http://www.simple-talk.com/opinion/geek-of-the-
week/rich-hickey-geek-of-the-week/
What is Clojure?

Clojure is a dynamic, LISP-like programming
       language that runs on the JVM
How does it look? Exactly! Like LISP
A form is a list where the first symbol in the
list has to be a special word that the
compiler can understand - usually the name
of a function
Clojure Features

● Functional
   ○ Immutable, persistent data structures
   ○ Functions are objects
● Lisp
● Hosted on JVM
   ○ the same memory model
   ○ garbage collector
   ○ etc
● Supporting Concurrency
● Open source
Clojure Features

● Dynamic development
   ○ REPL (read-eval-print-loop), on-the-fly
     compilation to JVM bytecode
● Full set of immutable, extensible, read-only
  data structures:
   ○ lists
   ○ maps
   ○ sets
   ○ vectors
● Macros
Clojure Features
                    Java interop



● Call java-methods, access fields
● Functions implement java.util.concurrent.
  Callable           (defn func (smthng))
● Proxy interfaces/classes
● Primitive types - int, long, byte, Char, String etc
● Clojure data structures implement Collection, Iterable,
  Comparable etc
Concurrent programming

● Things are happening at the same time
● Number of CPU is growing fast
Concurrent programming problems

Visibility problem
 ● Multiple threads use shared data
 ● 2 or more threads are trying to
   change the same data at the
   same time
Concurrent programming problems

Basic solution is
 ● locks
 ● synchronized access
 ● only one thread can have
   lock, others blocks
 ● But it requires additional efforts
     ○ thread-coordination
     ○ avoiding deadlocks
Concurrent programming problems

Access problem
 ● several threads are trying to
   access and change the same
   shared data at the same time
 ● write/read at the same time
 ● readers block readers
Clojure way

● There is no any possibility to
  change data by direct access. All
  data structures are immutable,
  remember?
● Only read, if we add element -
  new data structure will be
  created.
● No locks
● No access problems
But what if
we really want change
   data in Clojure?
     This opportunity is also
  provided by the language! All
     hard work done for us.
Reference types

● Vars
● Refs
● Atoms
● Agents
Vars

 ● Reference to mutable storage location
 ● Dynamically rebound on a per-thread basis
 ● Functions are vars, so they can be dynamically rebound too
 ● Ensure safe use via thread-isolation

(def x 5)              // root-binding
(x)                     // 5
....
(binding [x 20]
   (+ x 1))            // 21
....
(x)                     // 5 again, not changed
Changing Vars

(set! var-name new-value)
   ● cannot change root-binding
   ● works only in thread-local context (in "binding")
(def x 5)                  // root-binding
(set! x 7)                // exception will be thrown
....
(binding [x 20]
   (println (+ x 1))                // 21
   (set! x 10)
   (+ x 5))                         // 15
....
(x)                         // 5 again, not changed
Refs

● Based on Software Transactional Memory (STM)
● Change the value by applying a function to old value
● Refs can be changed within a transaction only
● Transactions will be retried automatically if conflict happens
● To make changes code must be surrounded with (dosync
  ...)
● All changes are atomic
Refs


       (def r (ref '(1 2 3)))
       (deref r)                   // list (1 2 3)

       (dosync                     // transaction begins
         ....
         (alter r                  // change ref!
              (fn [_] '(10 9 8))
         ...
       )                            // transaction ends

       (deref r)                   // list (10 9 8)
Refs lifecycle


    (def r (ref '(1 2 3)))
    (deref r)

    (dosync
      ....
                                if any other transaction
      (alter r
                                commutes - this
           (fn [_] '(10 9 8))
                                transaction is repeated
      ...
    )
                                Otherwise, that's ok and
    (deref r)                   programs continues
                                execution
Atoms

● Way to manage shared state synchronously
● Change the value by applying a function to old value
● This is done in an atomic manner by function swap!
● Internally, swap! reads the current value, appliesthe
  function to it, and attemprs to call compare-and-set! for this
  atom
Atoms best practices by Rich Hickey

(defn memoize [f]
 (let [mem (atom {})]
   (fn [& args]
     (if-let [e (find @mem args)]
       (val e)
       (let [ret (apply f args)]
         (swap! mem assoc args ret)
         ret)))))
Atoms best practices by Rich Hickey

(defn fib [n]
 (if (<= n 1)
   n
   (+ (fib (dec n)) (fib (- n 2)))))

(time (fib 35))
user=> "Elapsed time: 941.445 msecs"

(def fib (memoize fib))

(time (fib 35))

user=> "Elapsed time: 0.044 msecs"
Agents

● Asynchronous changing. They are not blocking main
  execution, return immediately
● Change the value by applying a function to old value
● Agent action dispatches take the
  form                      (send agent fn args*)
● If other actions try to change data - they will be added to
  the queue
Agents


(def a (agent 5))      // create agent
(deref a)               // 5

(send a
  (fn [old-val]
     (+ old-val 5)))
(deref a)              // may be 5, or may be 10
(Thread/sleep 1000)
(deref a)              // 10
Useful links

 ● https://groups.google.com/forum/#!forum/clojure
 ● http://clojure.org
 ● http://alexott.net/ru/clojure/index.html
 ● http://www.lisperati.com/clojure-spels/casting.html
 ● http://www.pluralsight-training.
   net/microsoft/courses/TableOfContents?
   courseName=clojure-concurrency-tutorial
Q&A

More Related Content

What's hot (20)

Clojure+ClojureScript Webapps
Clojure+ClojureScript WebappsClojure+ClojureScript Webapps
Clojure+ClojureScript Webapps
Falko Riemenschneider
 
Modxpo 2015 - Custom Manager Page in MODX Revolution
Modxpo 2015 - Custom Manager Page in MODX RevolutionModxpo 2015 - Custom Manager Page in MODX Revolution
Modxpo 2015 - Custom Manager Page in MODX Revolution
rtripault
 
Glusterd_thread_synchronization_using_urcu_lca2016
Glusterd_thread_synchronization_using_urcu_lca2016Glusterd_thread_synchronization_using_urcu_lca2016
Glusterd_thread_synchronization_using_urcu_lca2016
Atin Mukherjee
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Dive
davanum
 
Concurrency in Programming Languages
Concurrency in Programming LanguagesConcurrency in Programming Languages
Concurrency in Programming Languages
Yudong Li
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
Alex Miller
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Saeid Zebardast
 
ConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with GroovyConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with Groovy
Iván López Martín
 
Ast transformation
Ast transformationAst transformation
Ast transformation
Gagan Agrawal
 
'Getting' Clojure - '(parentheses are just hugs for your code)
'Getting' Clojure - '(parentheses are just hugs for your code)'Getting' Clojure - '(parentheses are just hugs for your code)
'Getting' Clojure - '(parentheses are just hugs for your code)
Gary Trakhman
 
CoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple wayCoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple way
Lim Chanmann
 
Polymorphism in c++
Polymorphism in c++Polymorphism in c++
Polymorphism in c++
Dheenadayalan18
 
Tomáš Čorej: Configuration management & CFEngine3
Tomáš Čorej: Configuration management & CFEngine3Tomáš Čorej: Configuration management & CFEngine3
Tomáš Čorej: Configuration management & CFEngine3
Jano Suchal
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
Abbas Raza
 
Swift Tutorial 2
Swift Tutorial  2Swift Tutorial  2
Swift Tutorial 2
Jintin Lin
 
Using VI Java from Scala
Using VI Java from ScalaUsing VI Java from Scala
Using VI Java from Scala
dcbriccetti
 
DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright
Andrei Alexandrescu
 
Java8 and Functional Programming
Java8 and Functional ProgrammingJava8 and Functional Programming
Java8 and Functional Programming
Yiguang Hu
 
Transactional Memory for Smalltalk
Transactional Memory for SmalltalkTransactional Memory for Smalltalk
Transactional Memory for Smalltalk
Lukas Renggli
 
CODEsign 2015
CODEsign 2015CODEsign 2015
CODEsign 2015
Max Kleiner
 
Modxpo 2015 - Custom Manager Page in MODX Revolution
Modxpo 2015 - Custom Manager Page in MODX RevolutionModxpo 2015 - Custom Manager Page in MODX Revolution
Modxpo 2015 - Custom Manager Page in MODX Revolution
rtripault
 
Glusterd_thread_synchronization_using_urcu_lca2016
Glusterd_thread_synchronization_using_urcu_lca2016Glusterd_thread_synchronization_using_urcu_lca2016
Glusterd_thread_synchronization_using_urcu_lca2016
Atin Mukherjee
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Dive
davanum
 
Concurrency in Programming Languages
Concurrency in Programming LanguagesConcurrency in Programming Languages
Concurrency in Programming Languages
Yudong Li
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
Alex Miller
 
ConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with GroovyConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with Groovy
Iván López Martín
 
'Getting' Clojure - '(parentheses are just hugs for your code)
'Getting' Clojure - '(parentheses are just hugs for your code)'Getting' Clojure - '(parentheses are just hugs for your code)
'Getting' Clojure - '(parentheses are just hugs for your code)
Gary Trakhman
 
CoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple wayCoffeeScript - JavaScript in a simple way
CoffeeScript - JavaScript in a simple way
Lim Chanmann
 
Tomáš Čorej: Configuration management & CFEngine3
Tomáš Čorej: Configuration management & CFEngine3Tomáš Čorej: Configuration management & CFEngine3
Tomáš Čorej: Configuration management & CFEngine3
Jano Suchal
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
Abbas Raza
 
Swift Tutorial 2
Swift Tutorial  2Swift Tutorial  2
Swift Tutorial 2
Jintin Lin
 
Using VI Java from Scala
Using VI Java from ScalaUsing VI Java from Scala
Using VI Java from Scala
dcbriccetti
 
DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright DConf 2016: Keynote by Walter Bright
DConf 2016: Keynote by Walter Bright
Andrei Alexandrescu
 
Java8 and Functional Programming
Java8 and Functional ProgrammingJava8 and Functional Programming
Java8 and Functional Programming
Yiguang Hu
 
Transactional Memory for Smalltalk
Transactional Memory for SmalltalkTransactional Memory for Smalltalk
Transactional Memory for Smalltalk
Lukas Renggli
 

Viewers also liked (6)

Evaluation of a CIS
Evaluation of a CISEvaluation of a CIS
Evaluation of a CIS
kylerossakers
 
Stm in-clojure
Stm in-clojureStm in-clojure
Stm in-clojure
Tom Van Cutsem
 
Sabrinappt
SabrinapptSabrinappt
Sabrinappt
Middle Tennessee State University
 
Presentation1
Presentation1Presentation1
Presentation1
nicolejamiee2
 

Similar to Clojure concurrency overview (20)

(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
Jacek Laskowski
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
Luke Donnet
 
Dragoncraft Architectural Overview
Dragoncraft Architectural OverviewDragoncraft Architectural Overview
Dragoncraft Architectural Overview
jessesanford
 
Clojure intro
Clojure introClojure intro
Clojure intro
Basav Nagur
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
Alina Dolgikh
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
Juan-Manuel Gimeno
 
Lcna 2012-tutorial
Lcna 2012-tutorialLcna 2012-tutorial
Lcna 2012-tutorial
Gluster.org
 
Lcna tutorial-2012
Lcna tutorial-2012Lcna tutorial-2012
Lcna tutorial-2012
Gluster.org
 
ClojureScript for the web
ClojureScript for the webClojureScript for the web
ClojureScript for the web
Michiel Borkent
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
C4Media
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
Jorg Janke
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
Michiel Borkent
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
John Stevenson
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
Nico Ludwig
 
Clojure and Modularity
Clojure and ModularityClojure and Modularity
Clojure and Modularity
elliando dias
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
Baishampayan Ghose
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
JAX London
 
Concurrency Constructs Overview
Concurrency Constructs OverviewConcurrency Constructs Overview
Concurrency Constructs Overview
stasimus
 
Clojure 1.1 And Beyond
Clojure 1.1 And BeyondClojure 1.1 And Beyond
Clojure 1.1 And Beyond
Mike Fogus
 
C language
C languageC language
C language
Robo India
 
(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
Jacek Laskowski
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
Luke Donnet
 
Dragoncraft Architectural Overview
Dragoncraft Architectural OverviewDragoncraft Architectural Overview
Dragoncraft Architectural Overview
jessesanford
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
Alina Dolgikh
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
Juan-Manuel Gimeno
 
Lcna 2012-tutorial
Lcna 2012-tutorialLcna 2012-tutorial
Lcna 2012-tutorial
Gluster.org
 
Lcna tutorial-2012
Lcna tutorial-2012Lcna tutorial-2012
Lcna tutorial-2012
Gluster.org
 
ClojureScript for the web
ClojureScript for the webClojureScript for the web
ClojureScript for the web
Michiel Borkent
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
C4Media
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
Jorg Janke
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
Michiel Borkent
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
John Stevenson
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
Nico Ludwig
 
Clojure and Modularity
Clojure and ModularityClojure and Modularity
Clojure and Modularity
elliando dias
 
Clojure made-simple - John Stevenson
Clojure made-simple - John StevensonClojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
JAX London
 
Concurrency Constructs Overview
Concurrency Constructs OverviewConcurrency Constructs Overview
Concurrency Constructs Overview
stasimus
 
Clojure 1.1 And Beyond
Clojure 1.1 And BeyondClojure 1.1 And Beyond
Clojure 1.1 And Beyond
Mike Fogus
 

Recently uploaded (20)

Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
 
Microsoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentationMicrosoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentation
Digitalmara
 
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Nikki Chapple
 
Content and eLearning Standards: Finding the Best Fit for Your-Training
Content and eLearning Standards: Finding the Best Fit for Your-TrainingContent and eLearning Standards: Finding the Best Fit for Your-Training
Content and eLearning Standards: Finding the Best Fit for Your-Training
Rustici Software
 
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptxECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
Jasper Oosterveld
 
Supercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMsSupercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMs
Francesco Corti
 
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Eugene Fidelin
 
Introducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and ARIntroducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and AR
Safe Software
 
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Aaryan Kansari
 
Building Agents with LangGraph & Gemini
Building Agents with LangGraph &  GeminiBuilding Agents with LangGraph &  Gemini
Building Agents with LangGraph & Gemini
HusseinMalikMammadli
 
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
James Anderson
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Introducing Ensemble Cloudlet vRouter
Introducing Ensemble  Cloudlet vRouterIntroducing Ensemble  Cloudlet vRouter
Introducing Ensemble Cloudlet vRouter
Adtran
 
Talk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya WeersTalk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya Weers
Kaya Weers
 
Security Operations and the Defense Analyst - Splunk Certificate
Security Operations and the Defense Analyst - Splunk CertificateSecurity Operations and the Defense Analyst - Splunk Certificate
Security Operations and the Defense Analyst - Splunk Certificate
VICTOR MAESTRE RAMIREZ
 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
 
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure ModesCognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Dr. Tathagat Varma
 
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Lorenzo Miniero
 
Dev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API WorkflowsDev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API Workflows
UiPathCommunity
 
Measuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI SuccessMeasuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI Success
Nikki Chapple
 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
 
Microsoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentationMicrosoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentation
Digitalmara
 
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Nikki Chapple
 
Content and eLearning Standards: Finding the Best Fit for Your-Training
Content and eLearning Standards: Finding the Best Fit for Your-TrainingContent and eLearning Standards: Finding the Best Fit for Your-Training
Content and eLearning Standards: Finding the Best Fit for Your-Training
Rustici Software
 
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptxECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
Jasper Oosterveld
 
Supercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMsSupercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMs
Francesco Corti
 
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Eugene Fidelin
 
Introducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and ARIntroducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and AR
Safe Software
 
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Aaryan Kansari
 
Building Agents with LangGraph & Gemini
Building Agents with LangGraph &  GeminiBuilding Agents with LangGraph &  Gemini
Building Agents with LangGraph & Gemini
HusseinMalikMammadli
 
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
James Anderson
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Introducing Ensemble Cloudlet vRouter
Introducing Ensemble  Cloudlet vRouterIntroducing Ensemble  Cloudlet vRouter
Introducing Ensemble Cloudlet vRouter
Adtran
 
Talk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya WeersTalk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya Weers
Kaya Weers
 
Security Operations and the Defense Analyst - Splunk Certificate
Security Operations and the Defense Analyst - Splunk CertificateSecurity Operations and the Defense Analyst - Splunk Certificate
Security Operations and the Defense Analyst - Splunk Certificate
VICTOR MAESTRE RAMIREZ
 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
 
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure ModesCognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Dr. Tathagat Varma
 
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Lorenzo Miniero
 
Dev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API WorkflowsDev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API Workflows
UiPathCommunity
 
Measuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI SuccessMeasuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI Success
Nikki Chapple
 

Clojure concurrency overview

  • 3. Overview Rich Hickey https://twitter.com/#!/richhickey -about 2.5 years working on Clojure -first public release at 2007 -interview: http://www.simple-talk.com/opinion/geek-of-the- week/rich-hickey-geek-of-the-week/
  • 4. What is Clojure? Clojure is a dynamic, LISP-like programming language that runs on the JVM
  • 5. How does it look? Exactly! Like LISP
  • 6. A form is a list where the first symbol in the list has to be a special word that the compiler can understand - usually the name of a function
  • 7. Clojure Features ● Functional ○ Immutable, persistent data structures ○ Functions are objects ● Lisp ● Hosted on JVM ○ the same memory model ○ garbage collector ○ etc ● Supporting Concurrency ● Open source
  • 8. Clojure Features ● Dynamic development ○ REPL (read-eval-print-loop), on-the-fly compilation to JVM bytecode ● Full set of immutable, extensible, read-only data structures: ○ lists ○ maps ○ sets ○ vectors ● Macros
  • 9. Clojure Features Java interop ● Call java-methods, access fields ● Functions implement java.util.concurrent. Callable (defn func (smthng)) ● Proxy interfaces/classes ● Primitive types - int, long, byte, Char, String etc ● Clojure data structures implement Collection, Iterable, Comparable etc
  • 10. Concurrent programming ● Things are happening at the same time ● Number of CPU is growing fast
  • 11. Concurrent programming problems Visibility problem ● Multiple threads use shared data ● 2 or more threads are trying to change the same data at the same time
  • 12. Concurrent programming problems Basic solution is ● locks ● synchronized access ● only one thread can have lock, others blocks ● But it requires additional efforts ○ thread-coordination ○ avoiding deadlocks
  • 13. Concurrent programming problems Access problem ● several threads are trying to access and change the same shared data at the same time ● write/read at the same time ● readers block readers
  • 14. Clojure way ● There is no any possibility to change data by direct access. All data structures are immutable, remember? ● Only read, if we add element - new data structure will be created. ● No locks ● No access problems
  • 15. But what if we really want change data in Clojure? This opportunity is also provided by the language! All hard work done for us.
  • 16. Reference types ● Vars ● Refs ● Atoms ● Agents
  • 17. Vars ● Reference to mutable storage location ● Dynamically rebound on a per-thread basis ● Functions are vars, so they can be dynamically rebound too ● Ensure safe use via thread-isolation (def x 5) // root-binding (x) // 5 .... (binding [x 20] (+ x 1)) // 21 .... (x) // 5 again, not changed
  • 18. Changing Vars (set! var-name new-value) ● cannot change root-binding ● works only in thread-local context (in "binding") (def x 5) // root-binding (set! x 7) // exception will be thrown .... (binding [x 20] (println (+ x 1)) // 21 (set! x 10) (+ x 5)) // 15 .... (x) // 5 again, not changed
  • 19. Refs ● Based on Software Transactional Memory (STM) ● Change the value by applying a function to old value ● Refs can be changed within a transaction only ● Transactions will be retried automatically if conflict happens ● To make changes code must be surrounded with (dosync ...) ● All changes are atomic
  • 20. Refs (def r (ref '(1 2 3))) (deref r) // list (1 2 3) (dosync // transaction begins .... (alter r // change ref! (fn [_] '(10 9 8)) ... ) // transaction ends (deref r) // list (10 9 8)
  • 21. Refs lifecycle (def r (ref '(1 2 3))) (deref r) (dosync .... if any other transaction (alter r commutes - this (fn [_] '(10 9 8)) transaction is repeated ... ) Otherwise, that's ok and (deref r) programs continues execution
  • 22. Atoms ● Way to manage shared state synchronously ● Change the value by applying a function to old value ● This is done in an atomic manner by function swap! ● Internally, swap! reads the current value, appliesthe function to it, and attemprs to call compare-and-set! for this atom
  • 23. Atoms best practices by Rich Hickey (defn memoize [f] (let [mem (atom {})] (fn [& args] (if-let [e (find @mem args)] (val e) (let [ret (apply f args)] (swap! mem assoc args ret) ret)))))
  • 24. Atoms best practices by Rich Hickey (defn fib [n] (if (<= n 1) n (+ (fib (dec n)) (fib (- n 2))))) (time (fib 35)) user=> "Elapsed time: 941.445 msecs" (def fib (memoize fib)) (time (fib 35)) user=> "Elapsed time: 0.044 msecs"
  • 25. Agents ● Asynchronous changing. They are not blocking main execution, return immediately ● Change the value by applying a function to old value ● Agent action dispatches take the form (send agent fn args*) ● If other actions try to change data - they will be added to the queue
  • 26. Agents (def a (agent 5)) // create agent (deref a) // 5 (send a (fn [old-val] (+ old-val 5))) (deref a) // may be 5, or may be 10 (Thread/sleep 1000) (deref a) // 10
  • 27. Useful links ● https://groups.google.com/forum/#!forum/clojure ● http://clojure.org ● http://alexott.net/ru/clojure/index.html ● http://www.lisperati.com/clojure-spels/casting.html ● http://www.pluralsight-training. net/microsoft/courses/TableOfContents? courseName=clojure-concurrency-tutorial
  • 28. Q&A