Skip to main content

Posts

Showing posts with the label Threads

Java Object - wait - notify - a practical example

Object is the super class of anything in Java. But, Most people fear the wait and notify methods in Object. The common java practitioner might not need to use these for all practical purposes. However, the fear is not because they are hard to understand, but because of the assumption and false notion (generally pushed from the *experts* to the newbies) that these are for *super users*. The easiest way to understand this is by example. Most examples include Produce Consumer, But in practice where is this applied? Some time back I wrote about Commons Pooling . Ever wonder how an Object pool Implemention, like this one , would time out on a blocking call without creating a new thread on its own? It uses a wait - notify[All] paradigm. To understand that Look at the source code of the said GenericObjectPool . The borrowObject method (line 911 - so apt??) which optionally waits for a maxWait long secs, is used like for (;;) { synchronized (this) { // Some Logic try { if (_maxWa...

Resource Contention in JEE for a layman

In terms of scalability, a frequent term used is "Resource Contention". Most commonly this means, there is a shared object, file handle, db connection, or similar for which Threads are trying to get ownership. Until they obtain this ownership they either wait or exit on a timeout mechanism. In normal flow, they simply wait - This state is called blocked state. In an asynchronous mode (generally meaning a spawned child thread), there can be a Time-Out flow that can be defined to control the maximum length of time the thread can take to complete a task. In a blocked state, There may be "Resource Contention" in one thread that is avoiding other threads to proceed. In a JEE environment, This Resource Contention is caused commonly by Transactions(, Synchronized blocks, Locks). Since User Threads are discouraged in JEE, Synchronized blocks and Locks are less often encountered as issues. That's theory. If you dint understand much, Dont feel despair. I come from the ver...