1
donuts
4y

When do you use an ExecutorService vs starting a Thread?

I tried executing a mongo.find() with service.submit() but it seems to start a new connection... or try to. The eventual error I got was something about state being closed.

Whereas thread.run() does not.

Comments
  • 0
    Note: This is me digging in my brain while being in a not so sane condition.

    Double check please.

    Afaik one instance of a MongoDB client can be passed to several threads, but several threads cannot create each one or more instances of MongoDB client.

    Hence, when you run with an Executor Service, the MongoDb client needs to be propagated.

    Otherwise you can get very funky errors
  • 0
    @IntrusionCM well my code is like

    MongoOpeeration ops = ...

    public Thread run() {
    Thread th = new Thread(() -> ops.find(...));

    th.run();

    return th;
    }

    But actually I tried it and it hangs for some other reason. I forget what tho...
Add Comment