Concurrent Programming in Java : Future Vs Fork/Join [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have learnt the concepts of Future ,Fork and Join. Can someone explain differences/similarities between these two. Where to apply which concept.

It's not really comparable.
A Future represents the result of an asynchronous computation and enables you to access the result of that computation when it's done (possibly in the future if the computation takes some time, hence the name).
A Fork/Join pool is a form of ExecutorService (task execution system) that executes its tasks using multiple threads and uses a work stealing algorithm. The result of a fork/join task is a Future.

Related

JavaFX Task vs plain old Thread, what are the differences? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Aside from the properties to know the state of the task and the call backs (completed, failed, etc), is there any other differences of using JavaFX Task over a plain old Java Thread with a lambda?
I'm not asking what's good or bad, I'm not asking what you think I should do, I'm asking for the objective factual differences that would happen when using a Task instead of a plain lambda to run a background thread in a JavaFX application. Aside from the the differences that I already mentioned.
On the Oracel's tutorial for concurrency, in the section titled "Why Use the javafx.concurrent Package?" it says:
If you have special requirements or need extra power over the code, implementing a background worker by creating a Runnable object and a new thread is an appropriate way to go.
other than that, all the reasons for using a Task are equally applicable to a Runnable and I don't see what special requirements or extra power one gains, I also don't see what you lose, if anything, aside from state and callbacks, when choosing Runnable over Task. What I do see is that using Task is much more verbose:
new Thread(this::doSomething).start();
vs
new Thread(new Task<Void>() {
#Override
protected Void call() throws Exception {
doSomething();
return null;
}
}).start();
My concern would be the some unexpected side effect from choosing a shorter more concise version of the code that according to Oracle, should only be used in special cases.
doSomething is a variety of different activity on this example and I'm interested in facts that are agnostic to what they do. In my case, it's mostly network requests that then update the UI by using Platoform.runLater.

java multithreading method that can acess atmost 3 threads concurrently [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Hi today in interview they asked a multi threading question that create a procedure in which there is a method getDBConnect() so that atmost 3 threads can access it concurrently. if 4th thraed try to access getDBConnect() method then 4th thread will go on wait state if anyone of 3 thread release the method getDBConnect() then 4th thread will access the getDBConnect() method using simple thread or executor thread.
Please help me to understand how can i make program of multi threading so that above criteria should be satisfied.
I'm not going to write code for you. But I can hint what this question is about. There is very interesting primitive of multithreading synchronization called Semaphore. JDK contains detailed description and sample of use https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html

print stack with changing values with Thread [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can I know when the thread was stoped and the processor moves to another thread..
Transition between threads harms my calculations, there is some way to know if the thread left and returned to my function?
You can not know when your thread was stopped and the system rescheduled another thread.
But - you can minimize the number of times this event happens by setting the affinity of your thread to one of the processors, and the affinities of the other threads to the other processors.
If you are using Linux, you can use taskset for each thread in the system (get the list by "ps -e") to set the affinities of the other threads to other processors.
This will decrease the load on the processor and will cause it to context-switch less times.
The simple answer is - you can't. Even if you could detect thread context switches it happens far too often to usefully logged.
A better question would be to look at why you need to know. If there is a problem there post it as a question and we can solve the real issue.

Difference between ReentrantLock and AbstractQueuedSynchronizer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can someone give me some examples to distinguish ReentrantLock and AbstractQueuedSynchronizer, under what circumstances, should I go for ReentrantLock, verse vice, when should I use AbstractQueuedSynchronizer.
when should I use AbstractQueuedSynchronizer
Probably never. Look at my answer in the link of the comments to see uses of AQS. AQS is a class that offers support for concurrent constructs. If you are not writing some kind of of lower level concurrency tool you should never use AQS. Most, if not all, barriers offered in j.u.c should suffice.
Can someone give me some examples to distinguish ReentrantLock and
AbstractQueuedSynchronizer
A better question is distinguish the differences between the ReentrantLock and
Semaphore
CyclicBarrier
ReentrantReadWriteLock
CountDownLatch
Each of these classes utilize AQS for concurrency support.

Does fork() call compute eventually? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My understanding is that it creates another thread and runs compute() in another thread.
then join fetches the result once recursively got it.
I would like to know whether fork() calls compute() or not. Thanks in advance.
The fork method will not call compute. It will push the forked task to a work queue in which the running thread determines if it should eventually call compute itself or notify other threads to steal that task and invoke compute.

Categories