concurrency

Use cases for Java concurrent utilities

I have read Java Concurrency in Practice and this is a great reference, but I would like to see a concise single page summary of the use cases of the java.util.concurrent package. For instance: Why use a concurrent collection over a synchronized collection? When should the atomic classes be preferred over explicit locking? When should...

JQuery Flickr API

Hi All! I am writing a script using jQuery and Flickr REST API. Now the problem statement: following the the pseudo algo hit Flickr API and get a list of photos using $.getJSON nad create li list elements create_gallery: function(){ $.getJSON( $.prep_api_url(), function(data){ $.each(data.photos.phot...

Multiple fork() Concurrency

How do you use the fork() command in such a way that you can spawn 10 processes and have them do a small task concurrently. Concurrent is the operative word, many places that show how to use fork only use one call to fork() in their demos. I thought you would use some kind of for loop but i tried and it seems in my tests that the fork()...

Is my spin lock implementation correct and optimal?

I'm using a spin lock to protect a very small critical section. Contention happens very rarely so a spin lock is more appropriate than a regular mutex. My current code is as follows, and assumes x86 and GCC: volatile int exclusion = 0; void lock() { while (__sync_lock_test_and_set(&exclusion, 1)) { // Do nothing. This GCC ...

Is concurrent computing important for web development?

Let's say I have a web application running on S servers with an average of C cores each. My application is processing an average of R requests at any instant. Assuming R is around 10 times larger than S * C, won't benefits from spreading the work of a request across multiple cores be minimal since each core is processing around 10 requ...

Java concurrency file system

Good day, I need to create a File System Manager (more or less) which can read or write data to files. My problem is how do I handle concurrency? I can do something like public class FileSystemManager { private ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); public byte[] read(String path) { readWriteLo...

Should I make all my java code threadsafe ?

I was reading some of the concurrency patterns in Brian Goetze's Java Concurrency in Practice and got confused over when is the right time to make the code thread safe. I normally write code that's meant to run in a single thread so I do not worry too much about thread safety and synchronization etc. However, there always exists a possi...

Deadlock in ThreadPoolExecutor

Encountered a situation when ThreadPoolExecutor is parked in execute(Runnable) function while all the ThreadPool threads are waiting in getTask func, workQueue is empty. Does anybody have any ideas? The ThreadPoolExecutor is created with ArrayBlockingQueue, corePoolSize == maximumPoolSize = 4 [Edit] To be more precise, the thread is...

How do I bind a NSProgressIndicator to a property of a NSOperation?

I've got a window which reflects the status of an NSOperation. How should I bind the NSProgressIndicator to the NSOperation's progress-property? ...

Accessing the JDBC ResultSet concurrently in Spring

I am processing a large amount of data in a Spring JDBC DAO. The DAO directly returns an Iterator over the objects which operates on a bounded BlockingQueue using take() while the retrieval operation is happening in a separate thread (using an ExecutorService). Inside this thread I see the following behaviour: the retrieval works but ce...

In Java, how do I wait for all tasks, but halt on first error?

I have a series of concurrent tasks to run. If any one of them fails, I want to interrupt them all and await termination. But assuming none of them fail, I want to wait for all of them to finish. ExecutorCompletionService seems like almost what I want here, but there doesn't appear to be a way to tell if all of my tasks are done, exce...

How to implement memcached using a database ?

As far as I know, memcached runs in-memory and does not have a persistent backing-store. Currently my team is not ready yet to use memcached. So, I plan to write a simple database backed alternative. My question is pretty much in similar vein to this other question http://stackoverflow.com/questions/33619/concurrent-logins-in-a-web-f...

SQL Express 2008 Not committing LINQ to SQL writes in sequence?

Hi, I've an event-driven app that has recently started using SQL Express 2008 (instead of Std version) in dev and test to store the current state of a state machine. After moving to SQL Express, we started seeing a discrepancy between the order in which we write to the database (using LINQ to SQL) and the order in which SQL Express Prof...

Single instantiation with arguments

I have a class (RInterfaceHL) that calls another class (JRIEngine) which provides native methods on a single-threaded application. Therefore I only want to have a single instance of my class (RInterfaceHL) per JVM. I can use the singleton pattern with static initialization to ensure only a single instantiation of RInterfaceHL, but RInt...

When to use pessimistic concurrency?

What are specific scenarios where pessimistic concurrency is used? ...

Shared Memory Semaphore

Hi, I have 10 processes running, each writing to the same file. I don't want multiple writers, so basically I am looking for a mutex/binary semaphore to protect file writes. The problem is I can't share the semaphore amongst 10 processes, so I am looking at using shared memory between 10 processes, and putting the semaphore inside share...

SQL last insert in Drupal. Is it really threadsafe?

I have a query that might be executed by several users consecutively. I'm scared that if I run the db_last_insert_id command, some users might not get the last insert id, due to concurrency. But according to: http://api.drupal.org/api/function/db%5Flast%5Finsert%5Fid/6, it sates: Returns the last insert id. This function is thread sa...

SwitchToThread/Thread.Yield vs. Thread.Sleep(0) vs. Thead.Sleep(1)

I am trying to write the ultimate "Yield" method to yield the current time slice to other threads. So far I have found that there are several different ways to make the thread yield its allocated time slice. I just want to make sure I am interpreting them correctly since the documentation is not very clear. So, from what I have read on s...

How to test visibility of values between threads

What is the best way to test value visibility between threads? class X { private volatile Object ref; public Object getRef() { return ref; } public void setRef(Object newRef) { this.ref = newRef; } } The class X exposes a reference to the ref object. If concurrent threads read and and write the object reference every Thread...

How to manage M threads (1 per task) ensuring only N threads at the same time. With N < M. In Java

I have a queue of task in java. This queue is in a table in the DB. I need to: 1 thread per task only No more than N threads running at the same time. This is because the threads have DB interaction and I don't want have a bunch of DB connections opened. I think I could do something like: final Semaphore semaphore = new Semaphore(N...