notes
  • computer-networking
    • extend-wifi-with-router
    • how-the-internet-works
    • idk
    • networking-devices
    • osi-model
    • tcp-ip
    • Types of VPN
  • databases
    • Foreign Keys
    • Redis
    • simple-queries
  • devops
    • ansible
    • Manual deployment
    • docker
    • Workflow file
    • nginx
    • promethues-grafana
    • terraform
  • hardware
    • Power
  • home-server
    • proxmox-basics
    • proxmox-setup
    • storage
  • languages-frameworks
    • programming-paradigms
    • programming-languages
      • Regex Notes
      • c
        • basics
        • pointers-memory
      • cpp
        • basics
        • running-cpp
      • php
        • basics
        • choizez
        • frameworks
          • laravel
      • python
        • venv
        • concepts
          • Using lambda
        • frameworks
          • django
            • django
            • start
      • java
        • advanced
          • functional-programming
          • reactive-programming
        • concepts
          • how-java-works
          • serialization
          • sockets
          • threads
        • extra
          • collection-framework
          • generics-and-wildcards
          • Regular Expressions (Regex)
          • streams
        • frameworks
          • orm
        • fundamentals
          • OOP
          • conditionals
          • data-structures
          • data-types
          • exceptions
          • files
          • Functions (aka method)
          • Loops
          • packages
          • type-casting
      • javascript
        • frameworks
          • morgan
          • Using Sequelize with PostgreSQL in JavaScript
  • operating-system
    • basics
    • linux-directories
    • Basic Unix Terminal Commands
  • others
    • dark-web
    • piracy
  • system-design
    • system-design
  • web-dev
    • full-stack
  • books
    • sicp
      • Recursion thought process
      • 1
        • 1.1
        • 1.2
        • 1.3
      • 2
        • 2.1
  • certifications
    • aws-certified-cloud-practitioner
      • core-services
      • other-services
    • comptia-a+
      • Cloud
      • hardware
      • Important terms
      • Important terms
      • Troubleshooting
  • cloud
    • aws
      • aws-cli
      • aws-ec2-deployment
  • dsa
    • algorithms
      • back-tracking
      • bfs
      • Binary Search
      • bit-manipulation
      • Bubble sort
      • bucket-sort
      • counting-sort
      • dfs
      • Divide & Conquer
      • djikstras-algorithm
      • dynamic-programming
      • external-sorting
      • greedy-algorithm
      • Heap sort
      • Insertion sort
      • kadanes-algorithm
      • Merge sort
      • Permutation
      • quick-sort
      • Radix Sort
      • recurrence-relation
      • recursion
      • Selection sort
      • sliding-window
      • subset
      • time-space-complexity
      • topological-sort
      • tree-traversals
      • Two Pointers Technique
    • data-structures
      • data-structures
  • security
    • authentication
      • What is JWT (JSON Web Token)?
    • software-architecture-design
      • design-patterns
Powered by GitBook
On this page
  • Ways to create a thread
  • When to use which implementation
  • Thread Lifecycle
  • Daemon Threads
  1. languages-frameworks
  2. programming-languages
  3. java
  4. concepts

threads

A thread is a lightweight, independent, and concurrent unit of execution within a program

Threads allow multiple tasks to be performed concurrently, and each stack has its own call stack & program counter

Ways to create a thread

  1. extends Thread

public class ThreadExtend extends Thread {

    public void run(){
        for (int i = 0; i<3 ; i++) {
            System.out.println("This is the class that extends Thread");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
  1. implements Runnable

public class ThreadImplementRunnable implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i<3 ; i++) {
            System.out.println("This is the class that implements Runnable interface!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

Both have similar implementations , but to use them ...

public class Main {
    public static void main(String[] args) throws InterruptedException {

        // Used when you want to create a specialized type of thread
        ThreadExtend threadExtend = new ThreadExtend();

        threadExtend.start();

        //  Use `implements Runnable` you are defining a task ThreadImplementRunnable() that can be executed by a thread
        ThreadImplementRunnable threadImplementRunnable = new ThreadImplementRunnable();
        Thread thread = new Thread(threadImplementRunnable);

        thread.start();
    }
}

When to use which implementation

extends Thread

Creates the functionality directly in a thread

Use this when you want to create a new type of thread with customized behavior

implements Runnable

Creates a functionality which can be used in the constructor of a thread

Use this when you want to define a task that can be executed by multiple threads without creating new thread types.

Thread Lifecycle

Daemon Threads

Daemon threads are low-priority threads that run in the background, providing services to non-daemon (user) threads.

The key distinction is that the JVM doesn't wait for daemon threads to finish before it exits. If all non-daemon (user) threads have completed, the JVM will exit, even if daemon threads are still running.

PrevioussocketsNextextra

Last updated 1 month ago

Thread Lifecycle