Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes Thread : Resource allocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread : Resource allocation" Watch "Thread : Resource allocation" New topic
Author

Thread : Resource allocation

Arnab Karmakar
Greenhorn

Joined: Jun 21, 2007
Posts: 11
If there are 10 threads who are trying to access connection object from database connection pool (5 instances in pool), how can it be implemented to ensure that none of the threads wait for any other thread to get a connection pool object.

I am not sure if it is at possible to ensure that all threads get access to connection object at the same time because there are only 5 instances in the pool (and 10 thread trying to access it).

Any insight will be appreciated.


Cheers
Arnab Karmakar
SCJP 1.4(100%) SCWCD 5(98%)
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
No, it is not possible.
If you have 5 cars and 10 drivers, then only 5 drivers can drive a car, the other drivers must wait.
The same is true for 5 connections and 10 threads.
Arnab Karmakar
Greenhorn

Joined: Jun 21, 2007
Posts: 11
Ireneusz Kordal wrote:No, it is not possible.
If you have 5 cars and 10 drivers, then only 5 drivers can drive a car, the other drivers must wait.
The same is true for 5 connections and 10 threads.


What if there are 5 thread and 5 connections in the pool. How can it be ensured that all gets access to an instance from the pool. If I make the getConnection() method synchronized, while thread 1 is trying to get a connection from pool (using getConnection() method), other threads will be waiting till Thread 1 finishes getting a connection. How can it be ensure that none waits for others?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Arnab Karmakar wrote:
What if there are 5 thread and 5 connections in the pool. How can it be ensured that all gets access to an instance from the pool. If I make the getConnection() method synchronized, while thread 1 is trying to get a connection from pool (using getConnection() method), other threads will be waiting till Thread 1 finishes getting a connection. How can it be ensure that none waits for others?


The time to get a lock is quick, measured in nano seconds. This is probably true to get the connection from the pool (assuming that it has been preallocated). So it is not an issue. It is highly unlikely that anyone will notice that a thread had to wait its turn to get a connection -- especially since after the connection is gotten, the thread will be doing a network / I/O bound operation to a database.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arnab Karmakar
Greenhorn

Joined: Jun 21, 2007
Posts: 11
Henry Wong wrote:
Arnab Karmakar wrote:
What if there are 5 thread and 5 connections in the pool. How can it be ensured that all gets access to an instance from the pool. If I make the getConnection() method synchronized, while thread 1 is trying to get a connection from pool (using getConnection() method), other threads will be waiting till Thread 1 finishes getting a connection. How can it be ensure that none waits for others?


The time to get a lock is quick, measured in nano seconds. This is probably true to get the connection from the pool (assuming that it has been preallocated). So it is not an issue. It is highly unlikely that anyone will notice that a thread had to wait its turn to get a connection -- especially since after the connection is gotten, the thread will be doing a network / I/O bound operation to a database.

Henry

You are considering the term "wait" as tolerance for a particular application. In some application 1 second might not be called as acceptable wait and in others 1 nano second might be called as too late. My question is, is it at all possible to provide Connection object from pool to the 5 threads at the same time without each thread waiting for others to get the object. That is to say, is it possible to do a lock free atomic operation.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Arnab Karmakar wrote:
That is to say, is it possible to do a lock free atomic operation.


Sure. That is what the atomic library is for... Now, of course, you'll have to check to see if your database library uses it.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread : Resource allocation
 
Similar Threads
Newbie to ThreadLocal
Threads and connection pool
Some Thread Synchornization questions ?
Socket reuse and security
Servlet, Threads, and connection pool