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 1 year ago.
Improve this question
I'm new to ForkJoinPool framework. Don't quite get it, how is that achieved that each thread in ForkJoinPool is certainly run by separate processor/core to provide real parallelism considering that there are many other threads outside the ForkJoinPool instance in the runtime executing concurrently. I have a clue that it has something to do with Thread Affinity. Can anyone share some ideas/links?
P.S. Of course, I meant the case when number of threads is no greater than Runtime.getRuntime().availableProcessors()
You asked:
how is that achieved that each thread in ForkJoinPool is certainly run by separate processor/core to provide real parallelism
There is no way within plain Java code to make certain cores run certain threads at certain times. A Java programmer has no direct control over parallelism.
When a Java thread is scheduled for execution on a CPU core, and for how long that execution runs, is up to the host OS thread technology being leveraged by your Java implementation.
As for processor affinity, also known as CPU pinning, see How to set a Java thread's cpu core affinity?. Beware the notice in Answer by rdalmeida:
… thread affinity is pointless unless you have previously isolated the core from kernel/user threads and hardware interrupts
Related
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 3 years ago.
Improve this question
I am writing a Java Application that aims to get and persist messages in real time. Beyond that, my application should care about several non-functional requirements like lineage control, transaction control, security, logging, monitoring, and so on.
Each feature is defined as a module and will be controlled within a thread concurring to my hardware resources (disk, memory, CPU, and GPU). As consequence, each time my application evolves, my thread control should evolves too.
To deal with that I am creating a global ExecutorService to manage all threads in my application. Some of these threads are permanent and defined as daemon. My application also control multiple sources. Each one representing a set of all features described above, evolving arbitrarily.
Which are the best practices for that scenery? How to control multiple threads while some will be created and dropped arbitrarily (as daemon or not) and another ones will be executed regularly nested or not to another threads?
Generally, if you do not want to manage creation and disposal of threads, use a thread pool (you can create one with ExecutorService executorService = Executors.newFixedThreadPool(10);).
However, be careful of premature optimisation (i.e. spending effort "optimising" code, at the expense of readability/maintainability, often when the performance gain is irrelevant). General good practice is:
Write clean, readable code.
Run your code and see if you encounter performance issues
Profile your code (e.g. with JProfiler or JVisualVM) to identify performance issues
Refactor your code if there is a significant problem with the way it is.
Remember, in general, developer time is more valuable than CPU time. Obviously there are limits to this, but usually you are best served keeping your code clean and readable.
You can also use this process to tweak parameters (e.g. on a fixed thread pool, there are various thresholds that can be fine-tuned to improve performance).
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 5 years ago.
Improve this question
I am trying to understand the multi-threading concepts. Does the support of multi-threading comes from:
1) From Operating system? (OR)
2) Language itself? (like Java if I am correct)
What is the role of CPU, does multi-threading capability is also due to CPU (not considering the multi-core processors)?
Can there be a scenario where OS or CPU isn't supporting the multi-threading but still possible with language itself?
Can anyone help me understand this?
Understanding java's native threads and the jvm has some information.
Basically, multithreading support comes from both the OS and Java in Java's case. This does not happen for, for instance, Python (the standard CPython has just a wrapper around Linux's threads).
https://superuser.com/questions/740611/what-is-the-difference-between-multithreading-and-hyperthreading details what a CPU does in order to multithread.
In theory, yes, a language can be the implementor of the threading. Depending on how you look at it, C doesn't count on the OS, it does it's own threading (mostly because the OS is written in C). The above link also says this.
The language doing its own threading may not be as efficient as OS-level threading, so OS-threading is preferred and commonly present.
A thread is a sequence of instructions that can be managed independently from other such sequences by a scheduler.
Usually, the scheduler is part of the operating system (for example, Linux's Completely Fair Scheduler).
In some approaches (for example, green threads, stackless Python), the scheduler is part of the language or the runtime environment.
In modern computing environments it is usually the case that the number of threads exceeds the number of CPU cores. This is usually handled through time slicing, when threads take turns running on the available hardware. It is the job of the scheduler to manage that.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
We know that a processor can process only one thread at a time. So when we say multitasking on a single processor, means the processor switches between the threads/ processes and gives the end-user a feeling of multitasking. Related to it, I've some questions to better understand the concept of multi-threading -
I think, totally it will take the same amount of time to execute all
the processes in switching manner and executing them one by one, am I
right?
If yes, is there any benefit of dividing a task in multiple
threads?
What does it mean, when we say quad-core, octa-core processor? Does it mean a system has 4, 8 processors respectively and it can process 4/ 8 threads at a time?
Yes, it will take the same amount of time. In fact, it will take a bit longer, because the switching takes time.
Dividing a task into multiple threads lets you use multiple processors.
Also, the CPU isn't always the slowest thing. Imagine you have a chat server - you could use one thread for each client, and each thread would spend most of its time doing nothing (waiting for the user to type a message).
A quad-core processor has 4 cores. An octa-core processor has 8 cores. Cores are pretty much separate processors, but on the same chip instead of separate chips.
1 Imagine your task is to read several files. If you run several reading threads it may significantly improve performance
2 Imagine your task is to calculate a sum of a large array of numbers, if you run 2 threads on 2 parts of the array in parallel, the speed will increase by 2
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
I was wondering if a simple program with no threads can run faster on some computers which have many cores? or on a cluster of linux servers?
Recently I have run my algorithm which has to process billions of IP packets on my PC(core i7 with 16GB RAM) and it took 1881 minutes to finish processing. Then I thought its good to run the algorithm on clusters of linux servers each node with 10 processors and 48GB RAM to get the results quicker. However, there is no big difference between the two experiments.
Can someone comments what I am missing?
Unless your algorithm actually makes use of those multiple instances and extra memory, there shouldn't be a lot of difference. Parallel programming is an art of its own, and a "regular", single-threaded program doesn't just change into parallel one by itself.
If you have a single thread of execution more cores, CPU or machines won't help. Only a faster CPU would speed things up, and that only if your process is CPU-bound, and not IO-bound.
First you should check, where your processing time is spent, in CPU, or waiting for IO. If you have a significant amount of CPU usage, you can try to parallelize your work, i.e. split the data into chunks, and have different thread resp. machines process them in parallel.
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 8 years ago.
Improve this question
I want to read and parse a lot of files. Since there are over 10000 files that are to be parsed, I want to make this process faster by making use of threads.
For example, if I had 5 threads, I want to have them all read a certain number of files concurrently so that the process of reading and parsing is faster. Is this possible? Would I gain any significant speed-up by splitting this up into threads? If so, how can I do this?
P.S. I am not against using external libraries.
I am working with jdk 1.6
If you have many files to read, the better approach is to have no more than one thread read each file. And the best way of handling many tasks with multiple threads , for most cases, is to use an ExecutorService that uses a thread pool. Submit a task to the service for each file to be read. Make the thread pool large enough to keep the I/O system busy (which is likely to be the bottleneck) and you will maximize performance.
See How to read all lines of a file in parallel in Java 8 for reading one file in parallel.
In your case, I'd just launch a pool of threads with as many threads as your process will allow, each with a "read the whole file" request for a file assigned to it, and let the OS decide which files to read in which order.