Multithreading in java || Methods in Thread || Java example

 

Java Multithreading :

Java multithreading is a concept of doing multiple task at a same time.Thread is a light weight sub-process.Let’s see a example of how this concept is used in real time.

 

Consider a two classes Abc() and Def().And will use for loop to print numbers from 0 to 9

class Abc {

    public void display(){

        for (int i = 0; i < 10; i++) {

            System.out.println("Abc " + i);
        }
    }
}

 

class Def {

    public void display(){

        for (int i = 0; i < 10; i++) {

            System.out.println("Def " + i);
        }
    }
}

 

now try to print the for loops

public class TestExample {

    public static void main(String args[]) {

        Abc abc = new Abc();
        abc.display();

        Def def = new Def();
        def.display();
    }
}

 

 java multithreading

 

Using threads:

Extend the classes Abc() and Def() with Thread class.

class Abc extends Thread {

    @Override
    public void run() {
        super.run();

        for (int i = 0; i < 10; i++) {

            System.out.println("Abc " + i);
        }
    }
}

 

class Def extends Thread {

    @Override
    public void run() {
        super.run();

        for (int i = 0; i < 10; i++) {

            System.out.println("Def " + i);
        }
    }

}

 

now start the threads and observer the output

 

public class TestExample {

    public static void main(String args[]) {

        Abc abc = new Abc();
        abc.start();

        Def def = new Def();
        def.start();
    }
}

 

 

 java multithreading

 

We can use methods in thread like start(), sleep(), stop(), getState(), getPriority(), getName().

 

start() :

Is used to start a thread, entry point for the thread we have used in the above example.

Abc abc = new Abc();
abc.start();

 

sleep() :

Will provide a delay in between the Thread execution. We need to provide time delay to sleep method.

sleep(time duartion);

always to be enclosed in try catch

try {
    sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

 

Example for sleep()

class Abc extends Thread {

    @Override
    public void run() {
        super.run();

        for (int i = 0; i < 10; i++) {

            System.out.println("Abc " + i);
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           
        }
    }
}

 

stop() :

This method is deprecated it was used to stop the thread, we should not stop the thread it will stop by itself.

class Abc extends Thread {

    @Override
    public void run() {
        super.run();

        for (int i = 0; i < 10; i++) {

            System.out.println("Abc " + i);

            if(i == 6)
                stop();

        }
    }
}

 

 java multithreading

 

getState() :

Get the state of  the thread

System.out.println("Thread state: "+abc.getState());

 java multithreading

 

getName():

Get the name of the thread

System.out.println("Thread name: "+abc.getName());

 java multithreading

 

getPriority() :

Get the priority of the thread

System.out.println("Thread priority: "+abc.getPriority());

 java multithreading

 

 

Show Buttons
Hide Buttons