How Synchronization works in Java?

Synchronization in java is done through aquiering the monitor on some specific Object. Therefore, if you do this: class TestClass { SomeClass someVariable; public void myMethod () { synchronized (someVariable) { … } } public void myOtherMethod() { synchronized (someVariable) { … } } } Then those two blocks will be protected by execution of … Read more

Spring @Async limit number of threads

If you are using Spring’s Java-configuration, your config class needs to implements AsyncConfigurer: @Configuration @EnableAsync public class AppConfig implements AsyncConfigurer { […] @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(5); executor.setQueueCapacity(50); executor.setThreadNamePrefix(“MyExecutor-“); executor.initialize(); return executor; } } See @EnableAsync documentation for more details : http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html

Difference between Interlocked.Exchange and Volatile.Write?

the Interlocked.Exchange uses a processor instruction that guarantees an atomic operation. The Volatile.Write does the same but it also includes a memory barrier operation. I think Microsoft added Volatile.Write on DotNet 4.5 due to support of ARM processors on Windows 8. Intel and ARM processors differs on memory operation reordering. On Intel, you have a … Read more

Simultaneous mutable access to arbitrary indices of a large vector that are guaranteed to be disjoint

You can sort indices_to_update and extract mutable references by calling split_*_mut. let len = big_vector_of_elements.len(); while has_things_to_do() { let mut tail = big_vector_of_elements.as_mut_slice(); let mut indices_to_update = compute_indices(); // I assumed compute_indices() returns unsorted vector // to highlight the importance of sorted order indices_to_update.sort(); let mut elems = Vec::new(); for idx in indices_to_update { // … Read more