0

the method ( transformAll ) transform all the img for a person and their friends. can we do a small modification to these two methods so that the imgs can be transformed in parallel ? for example using ParallelStream(). the transformImige() method is just a simple method no I/O is performed. Also, can we use streams in the transformAll() method?

Q : Is the class Person a valid implementation of the monitor pattern?

public static void transformAllFriends(Person p) {
        Set<Person> visited = new HashSet<>();
        transformAll(p, visited);

    }
public static void transformAll(Person p, Set<Person> visited) {

        p.transformImige();
        visited.add(p);
        for (Object pi : p.friends()) {
            if (visited.contains(pi)) {
                continue;
            }
            transfrmAll(p, visited);
        }

    }
lool
  • 21
  • 3
  • Are you sure about your inner call to `transfrmAll(p, visited);`? Looks like a never ending recursive call... – Jean-Baptiste Yunès Jun 16 '21 at 06:10
  • That is right, the point here is to see if it possible to parallelise these to method. – lool Jun 16 '21 at 06:14
  • [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – Slaw Jun 16 '21 at 06:19
  • @user15793316 right. – Jean-Baptiste Yunès Jun 16 '21 at 07:42
  • 1
    To answer the question “Is the class Person a valid implementation of the monitor pattern?”, it would be helpful if you included the code of the class Person. Besides that, the compiler is not forgiving spelling errors, so you can’t write `transfrmAll` to invoke the method `transformAll`. So this code has never seen a compiler, not to speak of being tested. Try to implement working sequential code first before worrying about parallel processing. – Holger Jun 16 '21 at 10:16
  • 1
    @user15793316 no, the code never adds a friend to the set. The recursive call passes the same `p` it received, which will always attempt to add the same person, while checking for the members of the `friends()` that it doesn’t add. – Holger Jun 16 '21 at 10:39
  • 1
    @user15793316 ok, so it’s a failure of doing something as simple as copy&paste of text. – Holger Jun 16 '21 at 11:00

0 Answers0