Skip to main content

How to create Thread in Java?

Here, program illustrates the use of Thread class for creating and running threads in an application. The program creates three threads A, B and C for undertaking three different tasks. The main method in the amit class also constitutes another thread which we may call the main thread.

The main thread dies at the end of its main method. However, before it dies, it creates and starts all the three threads A, B, and C. Note the statements like

            new A().start();

in the main thread. This is just a compact way of starting a thread. This is equivalent to:
            A aobj = new A();
            aobj.start();

Immediately after the thread A is started, there will be two threads running in the program: the main thread and the thread A. The start() method returns back to the main thread immediately after invoking the run() method, this allowing the main thread to start the thread B. Similarly it starts C thread. By the time the main thread has reached the end of its main method, there are a total of four separate threads running in parallel.

We have simply initiated three new threads and started them. We did not hold on to them any further. They are running concurrently on their own. Note that the output from the threads are not specially sequential. They do not follow any specific order. They are running independently of one another and each executes whenever it has a chance. Remember, once the threads are started, we cannot decide with certainty the order in which they may execute statements.

Program:

class A extends Thread
{
            public void run()
            {
                        for (int i = 1; i < = 5; i++)
                        {
                                    System.out.println(“From Thread A: i = “ + i);
                        }
                        System.out.println(“Exit from A”);
            }
}

class B extends Thread
{
            public void run()
            {
                        for (int j = 1; j < = 5; j++)
                        {
                                    System.out.println(“From Thread B: j = “ + j);
                        }
                        System.out.println(“Exit from B”);
            }
}

class C extends Thread
{
            public void run()
            {
                        for (int k = 1; k < = 5; k++)
                        {
                                    System.out.println(“From Thread C: k = “ + k);
                        }
                        System.out.println(“Exit from C”);
            }
}

class amit
{
            public static void main(String args[])
            {
                        new A().start();
                        new B().start();
                        new C().start();
            }
}

Output:





Resources Used:

1. E Balagurusamy
2. Java in 60 minutes a day
3. Complete Reference
4. Teach Yourself

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;    ...