java.util.concurrent

MultiThreading Vs ThreadPoolExecutor

你。 提交于 2019-12-07 00:42:13
问题 I have used multithreading in many of applications I wrote . While reading more I came across ThreadPoolExecutors . I couldn't not differentiate between the two scenario wise . Still what I understand is I should use multithreading when I have a task I want to divide a task in to multiple small tasks to utilize CPU and do the work faster . And use ThreadPoolExecutor when I have a set to tasks and each task can be run independent of each other. Please correct me if I am wrong . Thanks 回答1: A

What is the purpose of ScheduledFuture.get() method if is retrieved from the scheduleWithFixedDelay/scheduleAtFixedRate method

霸气de小男生 提交于 2019-12-06 19:27:09
问题 I am confused with the following I know, if I use the schedule method from the ScheduledThreadPoolExecutor class: ScheduledFuture<?> scheduledFuture = scheduledThreadPoolExecutor.schedule(myClassRunnable, 5, TimeUnit.SECONDS); I am able to retrieve later the value through scheduledFuture.get(5, TimeUnit.SECONDS) or scheduledFuture.get() and should be null because the task has been executed just only once and it is completed. And null because I am working with the shedule's Runnable method

Java example of using ExecutorService and PipedReader/PipedWriter (or PipedInputStream/PipedOutputStream) for consumer-producer

邮差的信 提交于 2019-12-06 14:58:57
问题 I'm looking for a simple producer - consumer implementation in Java and don't want to reinvent the wheel I couldn't find an example that uses both the new concurrency package and either of the Piped classes Is there an example for using both PipedInputStream and the new Java concurrency package for this? Is there a better way without using the Piped classes for such a task? 回答1: For your task it might be sufficient to just use a single thread and write to the file using a BufferedOutputStream

How does a Synchronized Collection Wrapper factory method “own” the object passed into it?

瘦欲@ 提交于 2019-12-06 06:36:27
问题 In the book Java Concurrency in Practice , Brian Goetz says that objects passed to constructors and methods of a class are not owned by the Class itself . Is it because they are coming from outside and the class has no control over them ? He goes on to say that there is an exception to this in cases where a method is explicitly designed to transfer ownership of objects passed in (such as Synchronized collection wrapper factory methods). Can some one give an example of the same and explain to

Is it appropriate to use AtomicReference.compareAndSet to set a reference to the results of a database call?

走远了吗. 提交于 2019-12-06 06:33:57
问题 I am implementing a simple cache with the cache stored as an AtomicReference. private AtomicReference<Map<String, String>> cacheData; The cache object should be populated (lazily) from a database table. I provide a method to return the cache data to a caller, but if the data is null (ie. not loaded), then the code needs to load the data from the database. To avoid synchronized I thought of using the compareAndSet() method: public Object getCacheData() { cacheData.compareAndSet(null,

Modifying values in ConcurrentHashMap

不打扰是莪最后的温柔 提交于 2019-12-06 04:29:06
In ConcurrentHashMap there is concept of segmentation. What it means if two threads are trying to access ConcurrentHashMap they it gets divided in two blocks and default size of blocks is 16. Now suppose in a scenario where ConcurrentHashMap has only two elements and two different threads comes and thread1 tries to modify first value and thread2 tries to modify second value. In this case whether ConcurrentHashMap will go for segmentation? Now in a different scenario both the threads try to modify same value how ConcurrentHashMap will handle this situation? By using locking mechanism or is

How does java.util.concurrent.Executor work?

假如想象 提交于 2019-12-06 04:04:45
问题 How does java.util.concurrent.Executor create the "real" thread? Suppose I am implementing Executor or using any executor service (like ThreadPoolExecutor). How does JVM internally work? 回答1: It calls ThreadFactory . Look at the Executors class. Note they all have an overloaded argument where you can supply a ThreadFactory implementation. The ThreadFactory interface is basically public Thread newThread(Runnable runnable); and the default implementation if not supplied basically just is return

Exception propagation in java.util.concurrent.CompletableFuture

Deadly 提交于 2019-12-06 03:38:54
There are two snippets of code. In the first one we create the CompletableFuture from the task which always throws some exception. Then we apply "exceptionally" method to this future, then "theAccept" method. We DO NOT assign new future returned by theAccept method to any variable. Then we invoke "join" on original future. What we see is that "exceptionally" method has been invoked as well as the "thenAccept". We see It because they printed appropriate lines in output. But the Exception has not been suppressed by "exceptionally" method. Suppress exception and provide us with some default value

Why there is no AtomicBooleanArray datatype in Java?

我的梦境 提交于 2019-12-06 03:22:07
问题 I have noticed that there is NO AtomicBooleanArray datatype in Java similar to the AtomicIntegerArray. Although I can use AtomicBoolean[] for my current needs, I was curious to get an understanding as to why AtomicBooleanArray is NOT part of the library. Any thoughts in this would be much appreciated. Thanks 回答1: AtomicBoolean actually wraps a int which is set to 0 or 1 for false or true. This is because it uses compareAndSwap methods which are int based, and not smaller. You could implement

List<Runnable> returned from shutdownNow() can not be converted to submitted Runnable

被刻印的时光 ゝ 提交于 2019-12-05 21:25:01
Below is the source code of the class. I wanted to verify how does shutdownNow() works for not submitted task. Problem I am getting in below code is shutdownNow() return List<FutureTask> and not List<Runnable> which I have submitted List<Runnable> containing submitted instance of PrimeProducer . In Below program I wanted to get the tasks which where not executed and their state so that I can reschedule them later. name() represents just state that I want to store. So I am not able to convert to submitted Task. class PrimeProducer implements Runnable { private final SynchronousQueue<BigInteger>