Java Thread & Synchronization : Basic introduction to thread

In this article we are going to learn about Java Thread & Synchronization, basic introduction. The theory part which is necessary for applying threading & synchronization. I will try to cover minimum information needed for understanding, detail you may find in Oracle's blogs or Java code geek blogs.

What is a Thread ?
A thread is a mini/light weight processing unit used by Run time Environment(JVM) to process which are isolated among each other. Why it is lightweight, because, a thread can't be independently full but it is part of a process. So, technically, a thread is a single processing unit for a whole task/process.

To have clear idea, lets introduce some related terms.

a. Multi tasking :  When CPU preforms multiple process from single user.
b. Multi processing : When a CPU performs multiple processes from multiple user
c. Multi Threading : When multiple threads performs under a single process to a CPU.

So, by architecture, a thread will have

1. Single process ID, name (or Identifier from Operating system)
2. Single resource unit shared among all threads under a process. The resource can be memory, CPU time. 

image

To know memory management details , you can visit my JVM architecture post.

Note : As you know, from CPU world , there is shared CPU core technology called Hyper Threading which is core sharing technology that enables two processes running under same CPU core. Please dont confuse with this. Hyper threading makes two logical processing unit for a single core which is actually recognized by OS as two processing unit. Where as, multiple thread (2 or more) is recognized as single process to OS which is handle by its own mechanism.

How thread works?
A thread is a implementation of runnable interface with run() method. So, when a thread is created, it actually initiate an execution unit in JVM which actually runs the statement declared in run(). Usually, when we create a thread, we need to call start() to start the thread and start() actually calls run() safely.
Thread can wait, join, stop for other threads.
A group of thread can work like as, 
Preemptive multithreading : In here, CPU time shared process where each thread get time to run in CPU(queued or randomly chosen or highest priority get CPU time first). This is fully controlled by JVM. 
Cooperative multithreading : in here, Highest priority always get CPU time first. Unless , we explicitly make unselfish, it behaves like as selfish multithreading. [yield() is used with same priority to make unselfish]
Note : In any java profiler, we can actually see the running thread. For visual VM, see the thread tab.
A thread dump contains currently running thread information. 

Life cycle : A thread generally have 4 State in its life cycle.
a. Ready
b. Running
c. Hold/Sleep
d. Stop/Death of a thread
image

here is how, thread changes its states :

From ready to running, it is done by start() method calls, which actually runs run() method.

From running to hold, it is done by wait() or sleep(ms) & eventually back to running by either time out or notify()/ notifyall()[for wait() calls only]

Note : Before jdk 1.2, suspend() and resume() was used , but it is deprecated due to thread safe.

From running to stop or death , calling stop() which is deprecated in . It is handled by JVM it self.

Why deprecated? you can find very good explanation in stackoverflow.

Type of thread : 
User Thread : The thread which are use for user tasks and managed by users, they are user thread. Stopping of a thread is done by users.
Demon thread : The thread which are managed by JVM it self and act like as service. (example, GC). It used to be low in priority due to it’s nature. Stopping of a thread is done by JVM.
We can change thread type but we can’t change type when thread started.

Important Properties of a thread  :
a. priority: (int, 1-10), representing priority of a particular thread to JVM. Default value 5 as normal. If it is created under a thread group, it gets the value from group. And, if the group default max value changed, it changed to max.
b. Name(char[]) : representing name ,
c. ID (long): Id of a thread.
d. threadStatus(int): shows thread stated or not
e. group (ThreadGroup) : Represents a set threads with common purposes, easy to manage all together.
All are private so, we can access by getters.
In the next post , we will see, how threads can be applied in application.

Please comment if you have any question.

Thanks.. :)

5 comments:

  1. Can you please brief on Heap Memory and JVM monitoring

    ReplyDelete
    Replies
    1. For heap, checkout JVM fundamentals. http://shantonusarker.blogspot.com/2015/07/a-quick-overview-on-jvm-architecture.html

      and for JVM monitoring, you can see use JMX or JVMTI(oracle JVM only) based tools , like visual VM, JMC. To monitor a JVM you need to enable JMX first and then you can connect remotely.

      Delete
  2. Your blog has given me that thing which I never expect to get from all over the websites. Nice post guys!

    ReplyDelete