Skip to main content
added 12 characters in body
Source Link
nakiya
  • 14.5k
  • 24
  • 87
  • 120

I have to rewrite this code using agents in someway the result of x is 0 (It means that each thread is executed one after one). But I have problems because I do not have enough knowledge about agents use. The original code is:

(def x 0) (let [ t1[t1 (Thread. #(dotimes [_ 10000] (def x (inc x))))  t2 (Thread. #(dotimes [_ 10000] (def x (dec x))))]  (.start t1)  (.start t2)  (.join t1)  (.join t2)  (println x)) 

When I want to use an agent with await(agent_name) to make each thread run separately, it does not work, the result is always different from zero. Please any suggestions about this?

I have to rewrite this code using agents in someway the result of x is 0 (It means that each thread is executed one after one). But I have problems because I do not have enough knowledge about agents use. The original code is:

(def x 0) (let [ t1 (Thread. #(dotimes [_ 10000] (def x (inc x)))) t2 (Thread. #(dotimes [_ 10000] (def x (dec x))))] (.start t1) (.start t2) (.join t1) (.join t2) (println x)) 

When I want to use an agent with await(agent_name) to make each thread run separately, it does not work, the result is always different from zero. Please any suggestions about this?

I have to rewrite this code using agents in someway the result of x is 0 (It means that each thread is executed one after one). But I have problems because I do not have enough knowledge about agents use. The original code is:

(def x 0) (let [t1 (Thread. #(dotimes [_ 10000] (def x (inc x))))  t2 (Thread. #(dotimes [_ 10000] (def x (dec x))))]  (.start t1)  (.start t2)  (.join t1)  (.join t2)  (println x)) 

When I want to use an agent with await(agent_name) to make each thread run separately, it does not work, the result is always different from zero. Please any suggestions about this?

edited body
Source Link

I have to rewrite this code using agents in someway the result of x is 0 (It means that each thread is executed one after one). But I have problems because I do not have enough knowledge about agents use. The original code is:

(def x 0) (let [ t1 (Thread. #(dotimes [_ 10000] (def x (inc x)))) t2 (Thread. #(dotimes [_ 10000] (def x (dec x))))] (.start t1) (.start t2) (.join t1) (.join t2) (println x)) 

When I want to use an agent with await(agent_name) to make each thread run separately, it does not work, the result is always different from cerozero. Please any suggestions about this?

I have to rewrite this code using agents in someway the result of x is 0 (It means that each thread is executed one after one). But I have problems because I do not have enough knowledge about agents use. The original code is:

(def x 0) (let [ t1 (Thread. #(dotimes [_ 10000] (def x (inc x)))) t2 (Thread. #(dotimes [_ 10000] (def x (dec x))))] (.start t1) (.start t2) (.join t1) (.join t2) (println x)) 

When I want to use an agent with await(agent_name) to make each thread run separately, it does not work, the result is always different from cero. Please any suggestions about this?

I have to rewrite this code using agents in someway the result of x is 0 (It means that each thread is executed one after one). But I have problems because I do not have enough knowledge about agents use. The original code is:

(def x 0) (let [ t1 (Thread. #(dotimes [_ 10000] (def x (inc x)))) t2 (Thread. #(dotimes [_ 10000] (def x (dec x))))] (.start t1) (.start t2) (.join t1) (.join t2) (println x)) 

When I want to use an agent with await(agent_name) to make each thread run separately, it does not work, the result is always different from zero. Please any suggestions about this?

Source Link

Agents and threads synchronization in Clojure

I have to rewrite this code using agents in someway the result of x is 0 (It means that each thread is executed one after one). But I have problems because I do not have enough knowledge about agents use. The original code is:

(def x 0) (let [ t1 (Thread. #(dotimes [_ 10000] (def x (inc x)))) t2 (Thread. #(dotimes [_ 10000] (def x (dec x))))] (.start t1) (.start t2) (.join t1) (.join t2) (println x)) 

When I want to use an agent with await(agent_name) to make each thread run separately, it does not work, the result is always different from cero. Please any suggestions about this?