Let’s do an experiment and see how many threads we can create: package Note, Java threads are just a thin wrapper of native threads. It requires system calls to create a native thread with the host OS. Besides, creating a thread has an overhead other than just allocating memory. Thus, even though we add more threads to the CPU, the effectiveness won’t increase rather, it will start degrading as we add more threads. While running only 4 at a time, the rest of them will be waiting. More threads mean that the thread scheduler would have to schedule them, and each will get less time. So 4 threads will run at a point in time. Secondly, let’s say we have 4 cores in our CPU, and as we know, the CPU runs one thread at a time, and the thread scheduler schedules the threads. That means if we run a Java process, we cannot create as many threads as we want because it is constrained by the available memory in the first place. When we create a thread, it allocates memory from the heap. In this article, I will try to come up with an answer via an exercise, so bear with me! But what does this leave us with? Can we create as many threads as we want? I wish I could answer this with a straight yes or no. In our previous Java threading articles in this series, we learned a great deal about how we can create threads and how to use them while avoiding the issues that come with them.
0 Comments
Leave a Reply. |