concurrency

Extending FutureTask, how to handle cancel

I've extended FutureTask from java.util.concurrent to provide callbacks to track the execution of tasks submitted to an ExecutorService. public class StatusTask<V> extends FutureTask<V> { private final ITaskStatusHandler<V> statusHandler; public StatusTask(Callable<V> callable, ITaskStatusHandler<V> statusHandler){ sup...

performance penalty of message passing as opposed to shared data

There is a lot of buzz these days about not using locks and using Message passing approaches like Erlang. Or about using immutable datastructures like in Functional programming vs. C++/Java. But what I am concerned with is the following: AFAIK, Erlang does not guarantee Message delivery. Messages might be lost. Won't the algorithm and...

How to handle Java "jobs" synchronously?

We have a set of actions or "Jobs" which we'd like to happen one at a time (not concurrently). Ie: Job A can't happen while B is happening, and you can't have two C jobs running at the same time. In the event that a thread attempts to run a job concurrently they should get an error. We shouldn't just queue up the request. Some jobs...

How to use PLINQ for IO-bound tasks?

According to the answer to this question, there is no way to effectively use LINQ for IO bound tasks. Is there a way to gain better control, or is LINQ just not suited for such tasks? ...

C# - List<> concurrent removing and adding

Hello, I am not too sure, so i thought i'd ask. Would removing and adding items to a System.Collections.Generic.List<> object be non-thread safe? My situation: When a connection is received, it is added to the list, but also at the same time, there's a worker that's removing dead connections and such. Is there a problem? Will a lock ...

When should one use actors to solve a concurrency problem?

I have been looking at the Actor Model for a while now. To me it seems to be just another approach towards concurrent programming with its own upsides and downsides. It certainly does not guarantee deadlock-free environment. A can wait for a message from B while B waits for a message from A. I don't know if we can say the actors approac...

Method to handle "killed" Java jobs?

Our server web app will handle jobs that are requested by REST API requests. Ideally if the server dies during a job (ie: plug pulled), the job should resume or restart at startup. A very convenient way to process these jobs is in a separate thread using some of the concurrent utility classes in Java 5. The only issue is, given a fa...

How much thread-safety is too much?

I've been reading Java Concurrency in Practice lately – great book. If you think you know how concurrency works, but then most of the time you face the real issues, it feels like SWAG is the most you can do, then this book will certainly shed some light on the topic. It's sort of scary how many things can actually go wrong when you try t...

What is the .NET equivalent of Java's java.util.concurrent package?

I come from a Java background. I am wanting to learn more about concurrency in .Net and C#. Is there something similar to Java's concurrent utils package? ...

Spring best practice for locking domain objects?

Using EJB entity beans you can configure the bean so that when a thread has access to an EJB entity bean, no other threads can access the EJB bean. The container will block other threads until the thread with the lock is finished with the bean. Is there a "Spring way" to do this? Or do you have to just use the standard Java concurrenc...

Using request.getSession() as a locking object?

I have some java code that gets and sets a session attribute: Object obj = session.getAttribute(TEST_ATTR); if (obj==null) { obj = new MyObject(); session.setAttribute(obj); } In order to make this code thread-safe, I'd like to wrap it in a synchronized block. But what do I use as the locking object? Does it make sense to use th...

Atomically mark and return a group of rows in database

I'm writing a background service that needs to process a series of jobs, stored as records in a sqlserver table. The service needs to find the oldest 20 jobs that need to be worked (where status = 'new'), mark them (set status = 'processing'), run them, and update the jobs afterward. It's the first part I need help with. There could be ...

What's the best way to divide large files in Python for multiprocessing?

I run across a lot of "embarrassingly parallel" projects I'd like to parallelize with the multiprocessing module. However, they often involve reading in huge files (greater than 2gb), processing them line by line, running basic calculations, and then writing results. What's the best way to split a file and process it using Python's multi...

NHibernate not throwing StaleObjectStateException when <Version> used and data changed in database

I have an entity mapped in NHibernate with optimistic concurrency control using a SQL timestamp column as the version number. The mapping is like the following: <class name="Entity" optimistic-lock="version" discriminator-value="0"> <id name="id"> <generator class="native" /> </id> <version name="Version" column="Ver...

C# how to protect the field of an atomic class?

I'm trying to make an AtomicReference class in C# and I want to keep the field reference protected, but I also need to return the value in the get method: class AtomicReference { private Object _value; public AtomicReference() { _value = new Object(); } public AtomicReference(Object value) { Opt...

C# How to make a generic class?

How can I make this generic? class AtomicReference { private Object _value; public AtomicReference() { _value = new Object(); } public AtomicReference(Object value) { OptimisticSet(value); } public Object CompareAndSet(Object newValue) { return Interlocked.Exchange(ref _valu...

Ordering of thread using ThreadPool.QueueUserWorkItem

hi guys, I'm new to threading basics. I have a queue of operations to be performed on a XML files(node add,node delete etc) 1]There are 'n' xml files and for each file a thread from thread pool is allocated using ThreadPool.QueueUserWorkItem to do those file operations. I want to achieve both concurrency and ordering of operation(im...

Php - Insert Autoincrement Value - For Parent/Child Tables - Concurrency Problem

Consider a simple schema of one to many relationship. Parent Table's Id is referenced in the Child table. In php I want to insert a row into the table using the statement mysql_query($query). Then I will get the id of the last inserted row by using mysql_insert_id(). Then i will use this id to insert the another row into the child's tab...

ThreadLocals hard to use

I'm using ThreadLocal variables (through Clojure's vars, but the following is the same for plain ThreadLocals in Java) and very often run into the issue that I can't be sure that a certain code path will be taken on the same thread or on another thread. For code under my control this is obviously not too big a problem, but for polymorphi...

How to asynchronously call a method in Java

Hi, I've been looking at Go's goroutines lately and thought it would be nice to have something similar in Java. As far as I've searched the common way to parallelize a method call is to do something like: final String x = "somethingelse"; new Thread(new Runnable() { public void run() { x.matches("something"); ...