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

  • Home
  • SEARCH
  • 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 7840621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:50:53+00:00 2026-06-02T15:50:53+00:00

I have this code: (defn on-thread [f] (.start (Thread. f))) (def myref (ref 0))

  • 0

I have this code:

(defn on-thread [f]
  (.start (Thread. f)))

(def myref (ref 0))

(on-thread
  #(loop [x 100]
     (when (> x 0)
       (do
         (Thread/sleep 100)
         (println "myref is:" @myref)
         (recur (dec x))))))

(on-thread 
  #(dosync
    (println "transaction1 initial:" @myref) 
    (alter myref inc)
    (println "transaction1 final:" @myref) 
    (Thread/sleep 5000)
    (println "transaction1 done")))

(on-thread 
  #(dosync
     (println "transaction2 initial:" @myref) 
     (Thread/sleep 1000)
     (alter myref inc)
     (println "transaction2 final:" @myref) 
     (println "transaction2 done")))

When I run this, it’s plain that the first transaction runs first, and it alters the ref’s value to 1 – but the other threads are don’t see this: okay, of course, because the first transaction hasn’t finished yet. So I think at this moment there were no “commit” yet back to the ref.

But at this time, the first transaction goes to sleep and while it’s sleeping, the second transaction tries to alter the ref’s value. And it’s get rolled back, and retried by the environment!
Why?
How the second transaction “see”, that something happened (or will be happening) with the ref by the first transaction?

I think it would be more logical if the second transaction would be able to alter the in-transaction value of the ref (from 0 to 1) and then sleep 1000, and then finally make a successful commit, then the first transaction would be retried.
But this is not the case.

Why?

  • 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-06-02T15:50:54+00:00Added an answer on June 2, 2026 at 3:50 pm

    The second transaction is retried because when it goes to alter your ref, it sees that it has already been altered by another non-committed transaction. Therefore it is retried until the first transaction commits.

    If by some chance the second transaction altered the ref first, then yes, the first transaction would be retried. However, that just isn’t the case because your second transaction occurs way later (in CPU time) after the first.

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

Sidebar

Related Questions

So I currently have this code: (ns contact-form.core (:gen-class)) (def foo Hello World!) (defn
have this code import threading def Thread(f): def decorator(*args,**kargs): print(args) thread = threading.Thread(target=f, args=args)
I am using clojure and hiccup (with noir) and I have this code: (defn
I have this code: <%= @r2_blog_posts = Refinery::Blog::Post.recent(2) %> <%= link_to(@r2_blog_posts.first) do %> <%=
I have this code : static dynamic Mean (dynamic x, dynamic y) { return
I have this code: $string = '/([\d,]+)\.(\d+)(.*)$/'; $condition = preg_replace($string, '$2', $price); if ($condition
I have this code: <a href=index.asp onclick=<%Session(example) = True%>>LINK</a> The problem is that this
I have this code that lets me create a custom UIBarButton which is inside
I have this code: cuts_html = ''; for( i = 0; i < attachments.length;
I have this code in jQuery, that I want to reimplement with the prototype

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.