Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

24. Multithreading in Java

 

Java Thread

In Java, threads can be created by using two mechanisms:

  1. Extending the Thread class
  2. Implementing the Runnable interface

Extending the Thread Class

  • We create a class that extends the java.lang.Thread class.
  • The java.lang package is imported automatically in out program.
  • This class overrides the run() method available in the Thread class.
  • A thread begins its life inside run() method.
  • We create an object of our class and call start() method to start the execution of a thread.
  • start() invokes the run() method on the Thread object.
class Run1 extends Thread {
    @Override
    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("I am watching coding video.");
            System.out.println("This is very nice video..");
        }
    }
}

class Run2 extends Thread {
    @Override
    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("I am listening song...");
            System.out.println("This is my favorite song....");
        }
    }
}

class ThreadClass {
    public static void main(String[] arr) {
        Run1 t1 = new Run1();
        Run2 t2 = new Run2();
        t1.start();
        t2.start();
    }
}

Output :

I am watching coding video.
This is very nice video..
I am watching coding video.
This is very nice video..
I am watching coding video.
This is very nice video..
I am watching coding video.
I am listening song...
This is very nice video..
I am watching coding video.
This is very nice video..
This is my favorite song....
I am listening song...
This is my favorite song....
I am listening song...
This is my favorite song....
I am listening song...
This is my favorite song....
I am listening song...
This is my favorite song....

Implementing the Runnable Interface

  • We create a new class which implements java.lang.Runnable interface.
  • The java.lang package is imported automatically in out program.
  • This class overrides the run() method available in the Runnable interface.
  • We instantiate a Thread class object and call start() method on this object.
class Run1 implements Runnable {
    @Override
    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("I am watching coding video.");
            System.out.println("This is very nice video..");
        }
    }
}

class Run2 implements Runnable {
    @Override
    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("I am listening song...");
            System.out.println("This is my favorite song....");
        }
    }
}

class RunnableInterface {
    public static void main(String[] arr) {
        Run1 r1 = new Run1();
        Run2 r2 = new Run2();
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        t1.start();
        t2.start();
    }
}

Output :

I am listening song...
This is my favorite song....
I am watching coding video.
I am listening song...
This is my favorite song....
I am listening song...
This is my favorite song....
I am listening song...
This is my favorite song....
This is very nice video..
I am watching coding video.
This is very nice video..
I am listening song...
This is my favorite song....
I am watching coding video.
This is very nice video..
I am watching coding video.
This is very nice video..
I am watching coding video.
This is very nice video..

Note: Thread class implements the Runnable interface and the run() method is declared inside the Runnable interface.
We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface.

Thread Priorities

  • Whenever we create a thread in Java, it always has some priority assigned to it.
  • Priorities in threads is a concept where each thread is having a priority which is represented by numbers ranging from 1 to 10.
  • In most cases, the thread scheduler schedules the threads according to their priority (known as primitive scheduling).
  • But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.
  • The default priority is set to 5 as expected.
  • Minimum priority is set to 1.
  • Maximum priority is set to 10.

Some of the methods in Thread class are as:

  • currentThread(): The method is used to return a reference to the currently executing thread object.
  • setName(): This method is used to change the name of the thread.
  • getName(): This method is used to return the name of the thread.
  • setPriority(): This method is used to change the name of the thread.
  • getPriority(): This method is used to return the priority of given thread.
  • getId(): This method returns the identifier of the invoked thread. The thread ID is unique, generated when thread is created and remains unchanged during its lifetime.
  • join(): The join() method permits one thread to wait until the other thread to finish its execution.
class A extends Thread {
    public void run() {
        System.out.println("Current Thread name is: " + Thread.currentThread().getName());
        System.out.println("Current Thread priority is: " + Thread.currentThread().getPriority());
    }
}

class ThreadPriority {
    public static void main(String[] arr) {
        A obj1 = new A();
        A obj2 = new A();
        A obj3 = new A();
        A obj4 = new A();
        A obj5 = new A();
        A obj6 = new A();
        obj1.setName("First");
        obj6.setName("Last");
        obj1.setPriority(Thread.MIN_PRIORITY);
        obj6.setPriority(Thread.MAX_PRIORITY);
        obj2.setPriority(2);
        obj3.setPriority(7);
        obj1.start();
        obj2.start();
        obj3.start();
        obj4.start();
        obj5.start();
        obj6.start();
    }
}

Output :

Current Thread name is: Thread-4
Current Thread name is: Thread-3
Current Thread name is: Thread-2
Current Thread name is: First
Current Thread priority is: 7
Current Thread priority is: 5
Current Thread priority is: 5
Current Thread priority is: 10
Current Thread name is: Last
Current Thread priority is: 2
Current Thread name is: Thread-5
Current Thread priority is: 1

Java Thread Constructors

Some of the main Constructors of Thread class are explained as:

  • Thread(): Allocates a new Thread object.
  • Thread(String name): Allocates a new Thread object and sets the name of the Thread.
  • Thread(Runnable target): Allocates a new Thread object.
  • Thread(Runnable target, String name): Allocates a new Thread object and sets the name of the Thread.
class Run1 extends Thread {
    public Run1(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println("I am extending Thread class.");
        System.out.println("This is inside Run1 class extending Thread class..");
    }
}

class Run2 implements Runnable {
    @Override
    public void run() {
        System.out.println("I am implementing Runnable interface...");
        System.out.println("This is inside Run2 class implementing Runnable interface....");
    }
}

class ThreadConstructors {
    public static void main(String[] arr) {
        Run1 t1 = new Run1("Deepak");
        t1.start();
        System.out.println("The name is " + t1.getName());
        System.out.println("The id is " + t1.getId());

        Run2 r2 = new Run2();
        Thread t2 = new Thread(r2, "Gagandeep");
        t2.start();
        System.out.println("The name is " + t2.getName());
        System.out.println("The id is " + t2.getId());
    }
}

Output :

I am extending Thread class.
This is inside Run1 class extending Thread class..
The name is Deepak
The id is 14
The name is Gagandeep
The id is 15
I am implementing Runnable interface...
This is inside Run2 class implementing Runnable interface....

Note: The above outputs in this page may vary in different runs depending on the JVM (Java Virtual Machine).

Previous

Next

Comments