Skip to main content

Introduction to Threads (JAVA)

Those who are familiar with the modern operating system such as Windows 7 may recognize that they can execute several programs simultaneously. This ability is known as multitasking. In system’s terminology, it is called multithreading.
Multithreading is a conceptual programming paradigm where a program (process) is divided into two or more subprograms (processes), which can be implemented at the same time in parallel. For example, one subprogram can display an animation on the screen while another may build the text animation. This is something similar to dividing a task into subtasks and assigning them to different people for execution independently and simultaneously.
In most of our computers, we have only a single processor and therefore, in reality, the processor is doing only one thing at a time. However, the processor switches between the processes so fast that it appears to human beings that all of them are being done simultaneously.
A thread is similar to a program that has a single flow of control. It has a beginning, a body, and an end, and executes commands sequentially. Every program will have at least one thread as shown in below figure.
A unique property of Java is its support for multithreading. That is, Java enables us to use multiple flows of control in developing programs. Each flow of control may be thought of as a separate tiny program (or module) known as thread that runs in parallel to others as shown in figure. A program that contains multiple flows of a control is known as multithreading program. The figure shown below illustrates a Java program with four threads, one main and three others. The main thread is actually the main method module, which is assigned to create and start the other threads, namely A, B and C.



Once initiated by the main thread, the threads A, B and C run concurrently (together) and share the resources jointly. It is like people living in joint families and sharing certain resources among all of them. The ability of a language to support multithreads is referred to as concurrency. Since threads in Java are subprogram of a main application program and share the same memory space, they are known as lightweight threads or lightweight processes.
It is important to remember that threads running in parallel does not really mean that they actually run at the same time. Since all the threads are running on a single processor, the flow of execution is shared between the threads. The Java interpreter handles the switching of control between the threads in such a way that it appears they are running concurrently.
Multithreading is a powerful programming tool that makes Java distinctly different from its fellow programming languages. Multithreading is useful in number of ways. It enables programmers to do multiple things at one time. They can divide a long program (containing operations that are conceptually concurrent) into threads and execute them in parallel. For example, we can send tasks such as printing into the background and continue to perform some other task in the foreground. This approach would considerably improve the speed of our programs.
Let us consider the life cycle of a thread. A thread moves through several states from its creation to its ultimate death. These possible states are:
1.      New
2.      Ready
3.      Running
4.      Waiting (Blocked)
5.      Dead (Exit)


A thread is in the new state immediately after it is created and transitions to the ready state after it is started. It moves to the running state when the JVM selects it for execution. If the thread completes, it transitions to the dead state. If it chooses to sleep, wait, or perform an I/O operation, it changes to the waiting state. When this sleep, wait, or I/O operation completes, the thread again changes to the ready state. The JVM may move a thread from the running to ready state so that another thread has an opportunity to execute.
One of the functions of the JVM is to schedule threads for execution. When several threads are ready, it selects one of these to execute. Each thread has a priority. The JVM selects the highest-priority thread in the ready state for execution. A thread does not execute if there are higher-priority threads in the ready state.
If severable runnable threads have the same priority, the JVM may timeslice the processor among them. After some period of time, the JVM preempts the thread that is currently executing and gives another thread an opportunity to execute. This behavior ensures that the processor time is equitably shared among these threads.
However, not all JVMs provide timeslicing. On those platforms, when a thread begins execution, it is not preempted unless a higher-priority thread becomes ready.

Resources Used:
1. Balagurusamy
2. Complete Reference
3. Java in 60 minutes
4. Teach Yourself in 24 hours

Compiled By: Chaudhary Amit V. 

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...

Switch-case, simple calculator . . .

11.      Write a program to perform some mathematic functions with the help of “switch-case” statement. #include<stdio.h> #include<conio.h> void main() { int a,b,ans; char c; clrscr(); printf("\ Enter a and b:"); scanf("%d%d",&a,&b); printf("\n Enter operator:"); fflush(stdin); scanf("%c",&c); switch(c) { case'+':ans=a+b;       break; case'-':ans=a-b;       break; case'*':ans=a*b;       break; case'/':ans=a/b;       break; } printf("\n ans %d",ans); getch(); } 12.      Print the Fibonacci series #include<stdio.h> #include<conio.h> void main() { int a=1,b=1,s=3,c,ch; clrscr(); printf("how many steps="); scanf("%d",&ch); printf("%5d%5d",a,b); for(s=3;s<=ch;s++) {       c=a+b;       printf("%5d",c);       a=b;    ...