java.util.concurrent

Java threads slow down towards the end of processing

风流意气都作罢 提交于 2019-12-01 06:42:42
I have a Java program that takes in a text file containing a list of text files and processes each line separately. To speed up the processing, I make use of threads using an ExecutorService with a FixedThreadPool with 24 threads. The machine has 24 cores and 48GB of RAM. The text file that I'm processing has 2.5 million lines. I find that for the first 2.3 million lines or so things run very well with high CPU utilization. However, beyond some point (at around the 2.3 lines), the performance degenerates with only a single CPU being utilized and my program pretty much grinding to a halt. I've

java.util.ConcurrentModificationException On MapView

随声附和 提交于 2019-12-01 06:40:36
问题 fellas I am facing very strange issue from many days. I am trying to update overlay frequently. So sometime I am getting "java.util.ConcurrentModificationException" when I touch on map or sometime getting when map trying to update overlay But I am not finding perfect line which on this error is coming. 02-17 14:56:01.621: W/dalvikvm(3653): threadid=1: thread exiting with uncaught exception (group=0x40015560) 02-17 14:56:01.631: E/AndroidRuntime(3653): FATAL EXCEPTION: main 02-17 14:56:01.631:

Synchronize Three Threads

谁说胖子不能爱 提交于 2019-11-30 15:16:18
Was asked this question in an interview , tried to solve it ... but not successful. I thought of using CyclicBarrier There are three threads T1 prints 1,4,7... T2 prints 2,5,8... and T3 prints 3,6,9 …. How do you synchronize these three to print sequence 1,2,3,4,5,6,7,8,9.... I tried writing & running the following code public class CyclicBarrierTest { public static void main(String[] args) { CyclicBarrier cBarrier = new CyclicBarrier(3); new Thread(new ThreadOne(cBarrier,1,10,"One")).start(); new Thread(new ThreadOne(cBarrier,2,10,"Two")).start(); new Thread(new ThreadOne(cBarrier,3,10,"Three

Need to manually synchronize the Synchronized list while iteration when it could be avoided?

余生颓废 提交于 2019-11-30 14:05:10
My question is about synchronizedList method Collections Class. Javadocs say: It is imperative that the user manually synchronize on the returned list when iterating over it: List list = Collections.synchronizedList(new ArrayList()); ... synchronized(list) { Iterator i = list.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } Though manually synchroniziation is not required for other methods. I looked into the source code of Collections class and found shyncronization has already been taken care for all methods like add public boolean add(E e) { synchronized(list

Why Lock condition await must hold the lock

为君一笑 提交于 2019-11-30 11:08:27
I am in doubt with that , in Java language, we need to acquire the lock, before we await some condition to be satisfied. For example, int java monitor lock: synchronized(lock){ System.out.println("before lock ..."); lock.wait(); System.out.println("after lock ..."); } or the concurrency utils: Lock lock = new ReentrantLock(); Condition cond = lock.newCondition(); lock.lock(); try{ System.out.println("before condition ..."); cond.await(); System.out.println("after condition ..."); }catch(Exception e){ e.printStackTrace(); }finally{ lock.unlock(); } So, why we can't await, without hold the lock

ForkJoinPool stalls during invokeAll/join

核能气质少年 提交于 2019-11-30 09:59:08
I try to use a ForkJoinPool to parallelize my CPU intensive calculations. My understanding of a ForkJoinPool is, that it continues to work as long as any task is available to be executed. Unfortunately I frequently observed worker threads idling/waiting, thus not all CPU are kept busy. Sometimes I even observed additional worker threads. I did not expect this, as I strictly tried to use non blocking tasks. My observation is very similar to those of ForkJoinPool seems to waste a thread . After debugging a lot into ForkJoinPool I have a guess: I used invokeAll() to distribute work over a list of

collecting from parallel stream in java 8

那年仲夏 提交于 2019-11-30 08:37:25
I want to take an input and apply parallel stream on that, then I want output as list. Input could be any List or any collection on which we can apply streams. My concerns here is that if we want output as map them we have an option from java is like list.parallelStream().collect(Collectors.toConcurrentMap(args)) But there is no option that I can see to collect from parallel stream in thread safe way to provide list as output. I see one more option there to use list.parallelStream().collect(Collectors.toCollection(<Concurrent Implementation>)) in this way we can provide various concurrent

Java ThreadPool usage

本秂侑毒 提交于 2019-11-30 07:25:09
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 specified URL, parses the HTML extracts links from it, and schedules unseen links back to frontier. A frontier is a queue of uncrawled URLs. The problem is how to write the get() method. If the queue is empty, it should wait until any URLCrawlers finish and then try again. It should return null only

Synchronize Three Threads

和自甴很熟 提交于 2019-11-29 21:05:21
问题 Was asked this question in an interview , tried to solve it ... but not successful. I thought of using CyclicBarrier There are three threads T1 prints 1,4,7... T2 prints 2,5,8... and T3 prints 3,6,9 …. How do you synchronize these three to print sequence 1,2,3,4,5,6,7,8,9.... I tried writing & running the following code public class CyclicBarrierTest { public static void main(String[] args) { CyclicBarrier cBarrier = new CyclicBarrier(3); new Thread(new ThreadOne(cBarrier,1,10,"One")).start()

Can I use the work-stealing behaviour of ForkJoinPool to avoid a thread starvation deadlock?

青春壹個敷衍的年華 提交于 2019-11-29 20:01:39
A thread starvation deadlock occurs in a normal thread pool if all the threads in the pool are waiting for queued tasks in the same pool to complete. ForkJoinPool avoids this problem by stealing work from other threads from inside the join() call, rather than simply waiting. For example: private static class ForkableTask extends RecursiveTask<Integer> { private final CyclicBarrier barrier; ForkableTask(CyclicBarrier barrier) { this.barrier = barrier; } @Override protected Integer compute() { try { barrier.await(); return 1; } catch (InterruptedException | BrokenBarrierException e) { throw new