27, ఆగస్టు 2022, శనివారం

CREATION OF THREADS IN JAVA

 

How to Create a Java Thread? 

Java lets you create thread in following two ways:- 

  • By implementing the Runnable interface.
  • By extending the Thread

Let’s see how both the ways help in implementing Java thread.

Runnable Interface

The easiest way to create a thread is to create a class that implements the Runnable interface.

To implement Runnable interface, a class need only implement a single method called run( ), which is declared like this:

1

public void run( )

Inside run( ), we will define the code that constitutes the new thread

Example:

1

2

3

4

5

public class MyClass implements Runnable {

public void run(){

System.out.println("MyClass running");

   }

}

To execute the run() method by a thread, pass an instance of MyClass to a Thread in its constructor(constructor in Java is a block of code similar to a method that’s called when an instance of an object is created). Here is how that is done:

1

2

Thread t1 = new Thread(new MyClass ());

t1.start();

When the thread is started it will call the run() method of the MyClass instance instead of executing its own run() method. The above example would print out the text “MyClass running“.

Extending Java Thread

The second way to create a thread is to create a new class that extends Thread, then override the run() method and then to create an instance of that class. The run() method is what is executed by the thread after you call start(). Here is an example of creating a Java Thread subclass:

1

2

3

4

5

public class MyClass extends Thread {

     public void run(){

     System.out.println("MyClass running");

   }

}

To create and start the above thread you can do like this:

1

2

MyClass t1 = new MyClass ();

T1.start();

When the run() method executes it will print out the text “MyClass running“.

So far, we have been using only two threads: the main thread and one child thread. However, our program can affect as many threads as it needs. Let’s see how we can create multiple threads.

Creating Multiple Threads

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

class MyThread implements Runnable {

String name;

Thread t;

    MyThread (String thread){

    name = threadname;

    t = new Thread(this, name);

System.out.println("New thread: " + t);

t.start();

}

 

 

public void run() {

 try {

     for(int i = 5; i > 0; i--) {

     System.out.println(name + ": " + i);

      Thread.sleep(1000);

}

}catch (InterruptedException e) {

     System.out.println(name + "Interrupted");

}

     System.out.println(name + " exiting.");

}

}

 

class MultiThread {

public static void main(String args[]) {

     new MyThread("One");

     new MyThread("Two");

     new NewThread("Three");

try {

     Thread.sleep(10000);

} catch (InterruptedException e) {

      System.out.println("Main thread Interrupted");

}

      System.out.println("Main thread exiting.");

      }

}

 

The output from this program is shown here:

 

New thread: Thread[One,5,main]

 New thread: Thread[Two,5,main]

 New thread: Thread[Three,5,main]

 One: 5

 Two: 5

 Three: 5

 One: 4

 Two: 4

 Three: 4

 One: 3

 Three: 3

 Two: 3

 One: 2

 Three: 2

 Two: 2

 One: 1

 Three: 1

 Two: 1

 One exiting.

 Two exiting.

 Three exiting.

 Main thread exiting.

This is how multithreading in java works.

 

 

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి