Tuesday, January 8, 2013

How the data stored in Java Threads?

The data in java is stored in various places.

In Java Virtual Machine each thread is awarded with a stack which is not allowed to access by other threads.

    • Stack
    • Heap
    • Method Area

Stack : Contains the local variables, parameters and return values of invoked methods for each thread. This stack information cannot be accessed by other threads. The stack also contains the Objects instance variables but not the objects itself.

Heap: The heap contains the objects which shall be shared across all the threads.

Method Area: This contains only the class variables (static variables) which are accessible by all threads.

Monday, January 7, 2013

Comparison of ThreadPoolExecutor Service in java.util.concurrent package

Comparison of various ThreadPoolExecutor service in java.util.current package.

Parameters

Description

CachedThreadPool

FixedThreadPool

ScheduledThreadPool

SingleThreadExecutor

SingleThread

ScheduledExecutor

CorePoolSize

No. of threads to be in pool even if they are idle.

0 mean no limit

User given value

User Given value

1

1

MaximumPoolSize

the maximum number of threads to allow in the pool

Integer.MAX_VALUE

Same value as CorePoolSize

Integer.MAX_VALUE

1

Integer.MAX_VALUE

KeepAliveTime

When the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.

60

0

0

0

0

Unit

the time unit for the keepAliveTime argument

SECONDS

MILLISECONDS

NANOSECONDS

MILLISECONDS

NANOSECONDS

WorkQueue

The queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.

SynchronousQueue

< Runnable>

LinkedBlockingQueue

<Runnable>

DelayedWorkQueue

LinkedBlockingQueue

<Runnable>

DelayedWorkQueue

In short SingleThreadExecutor is FixedThreadPoolExecutor with core pool size and max pool size as ‘1’.

SingleThreadScheduledExecutor is ScheduledExecutor with core poolsize as ‘1’.

Apart from above predefined executor services. Concurrent package gives flexibility to customize the parameters of the ThreadPool by using ThreadPoolExecutor class.

Example:

ExecutorService ex = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);