DEV Community

Anupam Tarai
Anupam Tarai

Posted on • Edited on

2 1 1 1 1

Thread In Java

  • A thread is a small part of a process capable of execute independently.
  • Java supports multithreading, allowing a process to be divided into multiple threads. These threads can be scheduled by the CPU to execute in parallel, improving the performance of the application.

Creation of a Thread

  • We can create a thread by extending the 'Thread' class or implementing the 'Runnable' interface. Override the run method inside the thread.

CODE

class A extends Thread{ //A is a thread that extends "Thread" class
    public void run(){ //Override the run method
        for (int i=0; i<10; i++){
        System.out.println("AAAA");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class B implements Runnable{ //B is a thread that impliments "Runnable" interface
    public void run(){ //Override the run method
        for (int i=0; i<10; i++){
            System.out.println("BB");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

public class Server {
   public static void main(String[] args){
        A a = new A();
        B b = new B();
        Thread Tb = new Thread(b); // we need to pass that runnable into the costructor of thread class to create a thread
        a.start();
       try {
           Thread.sleep(10);
       } catch (InterruptedException e) {
           throw new RuntimeException(e);
       }
       Tb.start();

    }
}

Output:
AAAA
AAAA
BB
BB
AAAA
AAAA
BB
AAAA
BB
BB
AAAA
BB
AAAA
AAAA
BB
BB
AAAA
BB
AAAA
BB

Enter fullscreen mode Exit fullscreen mode

Here we have two classes, A and B, both representing threads. When we start the threads using the start method, both threads begin executing in parallel.

Methods in the Thread Class

  • start(): Starts the thread's execution; the JVM calls the thread's run method.
  • run(): Contains the code to be executed by the thread.
  • join(): Waits for the thread to die. If called on a thread, the calling thread will wait until the thread on which join was called finishes.
  • sleep(long millis): Causes the current thread to sleep for the specified number of milliseconds.
  • interrupt(): Interrupts the thread, causing it to stop its current task and handle the interruption (if it checks for interruptions).

CONCLUSION

Threads are helpful in large applications that require more time and resources to execute. By dividing such applications into multiple threads and executing them in parallel, the performance of the application can be significantly increased.

Please comment below if I have made any mistakes or if you know any additional concepts related to this topic.

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay