Java Thread Basic - BEHIND JAVA

In the old days a computer had a single CPU, and was only capable of executing a single program at a time. Later came multitasking which meant that computers could execute multiple programs (AKA tasks or processes) at the same time. It wasn't really "at the same time" though. The single CPU was shared between the programs. The operating system would switch between the programs running, executing each of them for a little while before switching.

Along with multitasking came new challenges for software developers. Programs can no longer assume to have all the CPU time available, nor all memory or any other computer resources. A "good citizen" program should release all resources it is no longer using, so other programs can use them.

Later yet came multithreading which mean that you could have multiple threads of execution inside the same program. A thread of execution can be thought of as a CPU executing the program. When you have multiple threads executing the same program, it is like having multiple CPUs execute within the same program..

The multithreading refers two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. In java the thread operations are done by the class java.lang.Thread. It is possible to accommodate any number of threads in a java program. There is no limit for the number of thread. These threads can run concurrently either asynchronously or synchronously.

In Java architectures, the threads are encompassed within a single ‘process’ and they share the same address space, the same ‘heap’ but each thread has its own stack for local variables. Means each thread showing different stacktrace.

Concurrancy

Concurrency is the ability to run several programs or several parts of a program in parallel.

Process

A process runs independently and isolated of other processes. It cannot directly access shared data in other processes.

Thread

A thread is a so called lightweight process. It has its own call stack, but can access shared data of other threads in the same process.

Thread Lifecycle

The following diagram shows the lifecycle of a thread. (Please Note: According to sun, there is only 4 states new, runnable, non-runnable and terminated. There is no running state. But here i am using Running state because its better to understand :)

New

If an instance of thread create then the thread become new state. Then the thread become resides in the new state until the start(); method calls. but the start() method has not been invoked on the thread yet, thread is not eligible to run yet. In this state we can say Thread object is considered alive but thread is not alive yet.

Runnable

The thread is in runnable state after the invocation of start(); method. Runnable means just before going to run.

As soon as Thread enters runnable state it is eligible to run, but not running. (Thread scheduler has not scheduled the Thread execution yet, Thread has not entered in run() method yet)

A thread first enters the runnable state when the start() method is invoked, but a thread can also return to the runnable state after either running or coming back from a blocked, waiting, or sleeping state.

Running

The thread become in the running state from runnable state if it get the resources. In running state Thread starts executing by entering run() method. Thread scheduler selects thread from the runnable pool on basis of priority, if priority of two threads is same, threads are scheduled in unpredictable manner. Thread scheduler behaviour is completely unpredictable.

When threads are in running state, yield() method can make thread to go in Runnable state.

Waiting State

A thread that is waiting indefinitely for another thread to perform a particular action is in this state. Also a thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. But in this state Thread is still alive, but currently it’s not eligible to run. In other words.

By calling wait() method thread go from running to waiting state. In waiting state it will wait for other threads to release object monitor/lock.

Once notify() or notifyAll() method is called object monitor/lock becomes available and thread can again return to runnable state.

By calling sleep() method thread go from running to sleeping state. In sleeping state it will wait for sleep time to get over.

Suspend and resume Methods

Suspend() method can be used to put thread in waiting state and resume() method is the only way which could put thread in runnable state.

Terminate

A thread is considered dead when its run() method completes.
Once thread is dead it cannot be started again doing so will throw runtimeException i.e. IllegalThreadStateException.

destroy() method puts thread directly into dead state.

No comments:

Post a Comment

Pages