Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1022769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:30:52+00:00 2026-05-16T11:30:52+00:00

What is the difference between Clojure STM (dosync) approach and Java synchronize Block? I’m

  • 0

What is the difference between Clojure STM (dosync) approach and Java synchronize Block?

I’m reading the code below from “The sleeping barber” problem.
(http://www.bestinclass.dk/index.clj/2009/09/scala-vs-clojure-round-2-concurrency.html)

(defn the-shop [a]  
  (print "[k] entering shop" a)  
  (dosync     
    (if (< (count @queue) seats)  
      (alter queue conj a)  
      (print "[s] turning away customer" a))))

To avoid race conditions, dosync is used, so i ask myself “What is the difference (STM) from Java synchronize block” ? Will it block this critical code ?

Thanks in advance !
Dantas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:30:53+00:00Added an answer on May 16, 2026 at 11:30 am

    dosync and synchronized give access to completely different concurrency abstractions.

    synchronized is a way of acquiring and releasing locks. When a thread enters a synchronized block, it attempts to acquire the appropriate lock; if the lock is currently held by a different thread, the current thread blocks and waits for it to be released. This leads to certain problems, such as the risk of deadlock. The lock is released when the thread leaves the synchronized block.

    dosync marks a block of code which is to be run in a transaction. Transactions in Clojure are a way of coordinating changes to Refs (objects created with the ref function); if you need some code to have a consistent view of some pieces of mutable state in Clojure — and possibly change them — you put those in Refs and execute your code in a transaction.

    A transaction has the interesting property that it will restart if for some reason it cannot commit, up to a certain maximal number of retries (currently hard-coded to be 10000). Among the possible reasons for a transaction being unable to commit are an inability to obtain a consistent view of the world (actually, the relevant Refs — there is an “adaptive history” facility which makes this less of a problem than it might seem at first glance); simultaneous changes made by other transactions; etc.

    A transaction runs no risk of being deadlocked (unless the programmer goes out of their way to introduce a deadlock unrelated to the STM system through Java interop); livelock, on the other hand, is a certain possibility, though it is not very probable. In general, many — although not all! — of the intuitions programmers associate with database transactions are valid in the context of STM systems, including that of Clojure.

    STM is a huge topic; one excellent resource for learning about Clojure’s STM is Mark Volkmann’s Software Transactional Memory article. It goes into great depth in discussing Clojure’s STM in its final sections, but the beginning can serve as great introductory reading.

    As for the snippet you quoted, it’s actually not something you would normally want to emulate in production code, since dosync blocks should almost always be side-effect free; the print here can be useful for demonstrating the inner working of the STM, but if you wanted a transaction to cause side-effects in real code, you should have it spawn a Clojure Agent for the purpose (which would only execute its task if the transaction successfully commits).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You might want to check out something like acts_as_versioned instead… May 16, 2026 at 6:00 pm
  • Editorial Team
    Editorial Team added an answer wow. IE7 does not like grey, replaced it with #CCC… May 16, 2026 at 6:00 pm
  • Editorial Team
    Editorial Team added an answer If you try the following select * from sys.dm_os_waiting_tasks does… May 16, 2026 at 6:00 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

What is the difference between the evaluation of double and #'double in Clojure/Lisp? 1:2
Possible Duplicate: Difference between Convert.tostring() and .tostring() Hi Carrying on from this question What
I'm attempting to learn Clojure from the API and documentation available on the site.
I understand the conceptual difference between reduce and apply : (reduce + (list 1
I need help getting my head around the difference between my current OOP notion
What difference between adding library by add external jar and putting those jars to
Is there any difference between the Web Edition and Business Edition of Azure SQL
What is the difference between window.onload , document.onready and body.onload ?
What exactly is the difference between: scala> def foo = 5 foo: Int and
What is the difference between vb.net application and asp.net application? How can I publish

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.