java.util.concurrent

Lock a file while writing it on the disk

I have two independant threads F1 and F2 (to be precise, two instances of java.util.concurrent.FutureTask) that are running in parallel. F1 do some processing, and then copy the result in a XML file. Then, it repeats these steps until it has nothing to do (many XML files are created). F2 looks in the F1 output directory, and take one fi...

java.util.concurrent vs. Boost Threads library

Hi, How does the Boost Thread libraries compare against the java.util.concurrent libraries? Performance is critical and so I would prefer to stay with C++ (although Java is a lot faster these days). Given that I have to code in C++, what libraries exist to make threading easy and less error prone. I have heard recently that as of JDK...

java.util.ConcurrentModificationException in Non Multithreaded Program,

Hey SO Guru's im having one heck of a job with this code public void kill(double GrowthRate, int Death) { int before = population.size(); for (PopulationMember p : population) { int[] probs = ProbablityArrayDeath(GrowthRate,Death,(int)p.fitness()); if (probs[RandomNumberGen.nextRandomInt(0, 99)]==0) ...

is there java.concurrent.util (or equivalent) for WeakHashMap?

Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency? `Collections.synchronizedMap(new WeakHashMap<Class, Object>());` i.e. is there something from java.util.concurrent one can use instead? Note that merely replacing with new ConcurrentHashMap<Class, Object>...

How to learn about Threads, Especially in Java.

I have always been kind of confused by threads, and my class right now makes heavy use of them. We are using java.util.concurrent but I don't even really get the basics. UpDownLatch, Futures, Executors; these words just fly over my head. Can you guys suggest any resources to help learn what I need from the ground up? Thanks a lot in adv...

Long primitive or AtomicLong for a counter?

Hi I have a need for a counter of type long with the following requirements/facts: Incrementing the counter should take as little time as possible. The counter will only be written to by one thread. Reading from the counter will be done in another thread. The counter will be incremented regularly (as much as a few thousand times per s...

ConcurrentLinkedQueue$Node remains in heap after remove()

I have a multithreaded app writing and reading a ConcurrentLinkedQueue, which is conceptually used to back entries in a list/table. I originally used a ConcurrentHashMap for this, which worked well. A new requirement required tracking the order entries came in, so they could be removed in oldest first order, depending on some condition...

Android & java.util.concurrent.ConcurrentLinkedQueue

Hi All, I want to use ConcurrentLinkedQueue in an android application, have written the code, but now I'm getting an error when the project builds: Conversion to Dalvik format failed with error 2 I'm using Eclipse with the lastest version of the ADT plugin. Any ideas how I can fix this problem? ...

ScheduledThreadPoolExecutor executing a wrong time because of CPU time discrepancy

I'm scheduling a task using a ScheduledThreadPoolExecutor object. I use the following method: public ScheduledFuture<?> schedule(Runnable command, long delay,TimeUnit unit) and set the delay to 30 seconds (delay = 30,000 and unit=TimeUnit.MILLISECONDS). Sometimes my task occurs immediately and other times it takes 70 seconds. I be...

Thread safe Hash Map ?

Hi Guys, I am writing an application which will return a HashMap to user. User will get reference to this MAP. On the backend, I will be running some threads which will update the Map. What I have done so far? I have made all the backend threads so share a common channel to update the MAP. So at backend I am sure that concurrent wri...

How to give name to a callable Thread?

Hi Guys, I am executing a Callable Object using ExecutorService thread pool. I want to give a name to this thread. To be more specific, in older version I did this - Thread thread = new Thread(runnable Task); thread.setName("My Thread Name"); I use thread name in log4j logging, this helps a lot while troubleshooting. Now I am migrat...

Java ThreadPool usage

Dear StackOverflow, I'm trying to write a multithreaded web crawler. My main entry class has the following code: ExecutorService exec = Executors.newFixedThreadPool(numberOfCrawlers); while(true){ URL url = frontier.get(); if(url == null) return; exec.execute(new URLCrawler(this, url)); } The URLCrawler fetches the...

Are there any drawbacks with ConcurrentHashMap?

I need a HashMap that is accessible from multiple threads. There are two simple options, using a normal HashMap and synchronizing on it or using a ConcurrentHashMap. Since ConcurrentHashMap does not block on read operations it seems much better suited for my needs (almost exclusively reads, almost never updates). On the other hand, I ...

java.util.concurrent: Should I synchronize to avoid visiblity issues among threads?

I wonder if when using any of the java.util.concurrent classes I still need to synchronize access on the instance so to avoid visibility issues. In short the question is: When using an instance of java.util.concurrent, is it possible that one thread modify the instance (i.e., put an element in a concurrent hashmap) and a subsequent th...