banner



How Many Ways To Create Thread In Java

Introduction to Java Threads:

We had an in-depth look at Java Strings in our previous tutorial from this informative Series of Java tutorials.

In this tutorial, we are going to explore about,

  • What are threads?
  • How to create threads in Java?
  • Thread Methods
  • Thread Lifecycle

Java Threads with Methods

Here is a Video Tutorial on Java Thread:

What is 'Threads'?

Threads can help us to do parallel processing. Threads are useful when you want to run multiple pieces of code in parallel.

A thread can be defined as a lightweight process which can execute multiple codes in parallel. However, the thread is different from a process. In OS, for each process, a separate memory will be allocated. And the same is applicable for thread as well, it has separate memory. All the threads will run in the same memory which is allocated for the process.

How to create Threads in Java?

A Thread can be created in Java in the following ways:

  1. By Extending Thread class
  2. Implementing Runnable interface

By extending Thread class:

          public class PlayMusic extends Thread {  public void run() { for(int i=0;i<1000;i++) { System.out.println("Music Playing ...... "); } }  public static void main(String Args[]) {  PlayMusic p=new PlayMusic(); p.start();  for(int i=0;i<1000;i++) { System.out.println("coding"); } } }        

By extending Thread class

Implementing Runnable Interface:

          public class DemoThread implements Runnable{ public void run() { for(int i=0;i<1000;i++) { System.out.println("hey thread1 started"); } } public static void main(String[] args) { DemoThread d=new DemoThread(); Thread t1=new Thread(d); t1.start();  DownloadThread down =new DownloadThread(); Thread t2=new Thread(down); t2.start(); } }        

Implementing Runnable interface

Thread Methods:

start() – Starts the thread.
getState() – It returns the state of the thread.
getName() – It returns the name of the thread.
getPriority() – It returns the priority of the thread.
sleep() – Stop the thread for the specified time.
Join() – Stop the current thread until the called thread gets terminated.
isAlive() – Check if the thread is alive.

Thread Lifecycle:

Threads can go through five different status in its life cycle as shown below.

  1. New: When the thread instance is created, it will be in "New" state.
  2. Runnable: When the thread is started, it is called "Runnable" state.
  3. Running: When the thread is running, it is called "Running" state.
  4. Waiting: When the thread is put on hold or it is waiting for the other thread to complete, then that state will be known as "waiting" state.
  5. Terminated: When the thread is dead, it will be known as "terminated" state.
          public class ThreadMethodsDemo extends Thread { public void run() { for(int i=0;i<10;i++) { System.out.println("thread methods demo"); try { System.out.println("thread is going to sleep"); ThreadMethodsDemo.sleep(1000);  System.out.println("thread wake up"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) throws InterruptedException { ThreadMethodsDemo de = new ThreadMethodsDemo();  System.out.println("getstate1"+de.getState()); Runnable state de.start(); System.out.println("getstate2"+de.getState()); System.out.println("getstate3"+de.getState()); System.out.println("getstate4"+de.getState()); System.out.println("thread Name"+de.getName()); System.out.println("thread Priority"+de.getPriority()); System.out.println("getstate5"+de.getState()); } }        

Thread Life cycle

Key Points to be noted:

  • To execute multiple codes in parallel, we are going for threads.
  • You can create threads in two ways. Extending thread class and Implementing Runnable interface.
  • Thread status is new, runnable, running, waiting, and terminated.

Conclusion

In this tutorial, we explored Java Threads and how to create threads along with the various methods, and life cycle.

Further reading =>> Thread Testing Tutorial

Our upcoming tutorial will educate you more on basic IO operations in Java!!

PREV Tutorial | NEXT Tutorial

How Many Ways To Create Thread In Java

Source: https://www.softwaretestinghelp.com/java/java-threads/

Posted by: robertsonbeirch1984.blogspot.com

0 Response to "How Many Ways To Create Thread In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel