javalava javalava - 1 year ago 175
Java Question

vert.x and BIO threading

Lately I've started learning about

, non-blocking Java frameworks and there is one that caught my eye -

I guess the same question may apply to akka (Play framework) 'cause their philosophy or one of the goals is the same - and that is reducing thread number and therefore highering scalability of an application.

suggests that number of threads it consumes is ~equivalent to the number of CPU cores but it also bares in mind that sometimes you have to do a blocking operation so it encourages a developer to perform a blocking operation on a seperate working thread (worker vertical in terms of vert.x).

And that's where we come to my question:

  • if I'm supposed to perform blocking IO operations on a new separate
    thread that means that every user that does a blocking operation has
    a new thread and therefore number of threads again becomes equal to
    the number of users as in a classical blocking thread model?

A real example would be a JDBC query - if 1000 concurrent users query a SQL database via JDBC every user spawns it's own worker thread for that blocking operation. From my point of view there's no thread sparing, improved scalability or RAM sparing compared to classical blocking thread model. I must be missing something... Or not? Thank's in advance for all the answers.

Answer Source

There is no sense to spawn 1000 concurrent threads queryng an SQL database - just because typical database cannot bear so many simultaneous connections, typical number is 10-15. The solution is to organize a special thread pool, where each thread has an open connection and serves tasks which access database. Tasks are submitted to a common blocking queue, and working threads read from that queue in a loop. Tasks are instances of

interface DBRunnable {
  public void run(Connection conn);

working threads pass their connection to the tasks:

public void run() {
  Connection conn = DriverManager.getConnection(...);
  while (true) {
     DBRunnable task=queue.take();;