Good day !

There is a code:

for (int zoomLvl = 0; zoomLvl <= ImageZoomQuantity; zoomLvl++) { // некий код, из которого я получаю изображение for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { // каждая итерация возвращает поделенную часть от изображения, которое получил выше } } } 

From the first cycle I get an image and in the second and third cycle I divide this image into parts. I want to make a stream from each iteration of the first cycle.

Is there a way to break the for loop into streams? And is this a good idea?

    1 answer 1

    A slightly altered look, but the same logic. submit () returns a future that can be used in the future (if necessary). (Contains Java 8 code).

     ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); for (Image image : images) { executor.submit(() -> { Image subImage = // code to process image return subImage; }); } 

    It all depends on what parts of the original image are then used for. In most cases, this can also be done in the same stream that broke the picture. This is just one of many examples of dividing logic into threads. For example, if you use the GUI, the code will be slightly different (SwingWorker, Task, AsyncTask, depending on the framework). As for a good idea, if there is a need for faster processing of your image parts, then the threads will speed up this process. If the program is already running and the speed is sufficient for its main use, then there is no particular need to change something.

    • So to speed up, I started it. I make parts of the original image into squares in the second cycle (indicated in the comments). I am very poorly versed in this topic (It is new for me. Everywhere there are simple examples (I understand them), but how to redo your code is not very. Can you advise where to start? What can I read? - kxko
    • In this case, threads will really speed up your program. All you have to do is adapt the algorithm to run in multiple threads - AlmasB
    • Do not bother you go to the chat? - kxko
    • Unfortunately, I do not know Russian-language resources. Here is one of the best - docs.oracle.com/javase/tutorial/essential/concurrency/… . As for the chat, you can invite - AlmasB
    • Go here! - kxko