Multi Threading Programming (Java)

Multi Threading Programming In Java

What is the multi threading programming that we used is Java, hear this.. Java is actually a multi-threaded programming language and by that Java can let us develop a multi-threaded program.

Do you know what a multi-threaded program is? well a multi-threaded program contains two or more than two parts. Which runs concurrently and each of that one part can handle different task (i.e codes that contains methods, constructors etc) at the same equal time. This can actually improves the optimal use of the available resource, if you have multiple CPUs.

Basically multi threading programming takes the idea of multi tasking. Which is “doing more than 1 activities at the same time”. Multi tasking in our terms it means when a multiple processes share a common process resource such as a CPU.

Life Cycle of a Thread

Look at this 5 cycle of a Thread : First of all is New Thread. In this cycle a thread begins its circle of life / life cycle in a new state. It will not change to another state until the program starts the thread. Another name that I found is a little bit funny, it is ” born thread”.

  • Runnable – After a ” born thread” is started in its program. The thread becomes “runnable”, A thread in this kind of state is considered to be executing/doing its task.
  • Waiting State . Sometimes, the transitions of a thread state while the thread is waiting for another thread to perform its task . A thread transitions back to “runnable” state is only when another thread signals to another thread. That thread is the waiting thread to continue executing.
  • Timed Waiting – A thread that is in a runnable state can enter “timed waiting state” for a specified interval of time. The thread that is still in this state transitions back to “runnable state” when the time interval expires or when an event of its is waiting to occurs.
  • Terminated(Dead) – After all of those life cycle, the thread come to the of its circle of life and enter the terminated state when the thread completes its task or otherwise terminates.

Thread Priorities

Every Java thread that is created has a priority level that helps the operating system easily determine the order in which the threads have already been scheduled.
Java Thread priorities are in the range of minimum (MIN_PRIORITY (a constant of 1)) and a maximum (MAX_PRIORITY (a constant of 10)). By its default, every thread is given priority level of a normal (NORM_PRIORITY (a constant of 5)).
Threads with a higher priority are much more important to a program and should be allocated processor time. First thing before the thread with a lower-priority. But however, thread priorities does not / can not guarantee the order in which threads execute and are very much platform dependent.

 

One thought on “Multi Threading Programming (Java)”

Leave a Reply

Your email address will not be published. Required fields are marked *