DEV Community

Cover image for Thread in Java
Ricardo Maia
Ricardo Maia

Posted on

Thread in Java

๐—ง๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐˜€ in Java are a way to execute tasks concurrently within a program. A thread is essentially a path of execution within a program. When you create multiple threads, you can run several operations simultaneously, which is particularly useful for tasks involving I/O operations, such as file reading or network operations, or for improving the performance of applications that execute multiple independent tasks.

๐—ž๐—ฒ๐˜† ๐—–๐—ผ๐—ป๐—ฐ๐—ฒ๐—ฝ๐˜๐˜€ ๐—ฎ๐—ฏ๐—ผ๐˜‚๐˜ ๐—ง๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐˜€ ๐—ถ๐—ป ๐—๐—ฎ๐˜ƒ๐—ฎ

  1. ๐— ๐—ฎ๐—ถ๐—ป ๐—ง๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ:

    • Every Java application starts with a single thread called the ๐™ข๐™–๐™ž๐™ฃ ๐™ฉ๐™๐™ง๐™š๐™–๐™™, which is automatically created by the JVM (Java Virtual Machine) when the program begins.
  2. ๐—–๐—ฟ๐—ฒ๐—ฎ๐˜๐—ถ๐—ป๐—ด ๐—ง๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐˜€:
    There are two main ways to create threads in Java:

  • ๐—˜๐˜…๐˜๐—ฒ๐—ป๐—ฑ๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ง๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ ๐—ฐ๐—น๐—ฎ๐˜€๐˜€: You can create a new class that extends Thread and overrides the run() method.

Image description

  • ๐—œ๐—บ๐—ฝ๐—น๐—ฒ๐—บ๐—ฒ๐—ป๐˜๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ฅ๐˜‚๐—ป๐—ป๐—ฎ๐—ฏ๐—น๐—ฒ ๐—ถ๐—ป๐˜๐—ฒ๐—ฟ๐—ณ๐—ฎ๐—ฐ๐—ฒ: Another approach is to implement the ๐˜™๐˜ถ๐˜ฏ๐˜ฏ๐˜ข๐˜ฃ๐˜ญ๐˜ฆ interface and pass an instance of the class that implements ๐˜™๐˜ถ๐˜ฏ๐˜ฏ๐˜ข๐˜ฃ๐˜ญ๐˜ฆ to a Thread object.

Image description

  1. ๐™ง๐™ช๐™ฃ() ๐™–๐™ฃ๐™™ ๐™จ๐™ฉ๐™–๐™ง๐™ฉ() ๐™ˆ๐™š๐™ฉ๐™๐™ค๐™™๐™จ:

    • The ๐š›๐šž๐š—() method contains the code that the thread will execute. When you call the ๐šœ๐š๐šŠ๐š›๐š() method of a thread, it causes the JVM to create a new thread of execution and call the ๐š›๐šž๐š—() method in that new thread. If you call ๐š›๐šž๐š—() directly, it will run in the current thread without creating a new thread.
  2. ๐—ง๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ ๐—ฆ๐˜๐—ฎ๐˜๐—ฒ๐˜€:

    • ๐—ก๐—ฒ๐˜„: The thread has been created but not yet started.
    • ๐—ฅ๐˜‚๐—ป๐—ป๐—ฎ๐—ฏ๐—น๐—ฒ: The thread is ready to run but may be waiting for its turn to execute.
    • ๐—•๐—น๐—ผ๐—ฐ๐—ธ๐—ฒ๐—ฑ: The thread is blocked waiting for a monitor lock.
    • ๐—ช๐—ฎ๐—ถ๐˜๐—ถ๐—ป๐—ด/๐—ง๐—ถ๐—บ๐—ฒ๐—ฑ ๐—ช๐—ฎ๐—ถ๐˜๐—ถ๐—ป๐—ด: The thread is waiting indefinitely or for a specified time until another thread notifies.
    • ๐—ง๐—ฒ๐—ฟ๐—บ๐—ถ๐—ป๐—ฎ๐˜๐—ฒ๐—ฑ: The thread has finished execution.
  3. ๐—ฆ๐˜†๐—ป๐—ฐ๐—ต๐—ฟ๐—ผ๐—ป๐—ถ๐˜‡๐—ฎ๐˜๐—ถ๐—ผ๐—ป:

    • When multiple threads access and modify shared data, race conditions can occur. To prevent this, Java offers the concept of s๐šข๐š—๐šŒ๐š‘๐š›๐š˜๐š—๐š’๐šฃ๐šŠ๐š๐š’๐š˜๐š— using the ๐šœ๐šข๐š—๐šŒ๐š‘๐š›๐š˜๐š—๐š’๐šฃ๐šŽ๐š keyword, which can be applied to methods or code blocks.

Image description

  1. ๐—ง๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ ๐—–๐—ผ๐—บ๐—บ๐˜‚๐—ป๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป:

    • Java provides methods like ๐š ๐šŠ๐š’๐š(), ๐š—๐š˜๐š๐š’๐š๐šข(), and ๐š—๐š˜๐š๐š’๐š๐šข๐™ฐ๐š•๐š•() to allow threads to communicate with each other, especially useful for synchronizing threads in producer/consumer scenarios.
  2. ๐—˜๐˜…๐—ฒ๐—ฐ๐˜‚๐˜๐—ผ๐—ฟ๐˜€ ๐—ฎ๐—ป๐—ฑ ๐—ง๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ ๐—ฃ๐—ผ๐—ผ๐—น๐˜€:

    • To manage a large number of threads, Java offers the java.util.concurrent framework, which includes classes like ExecutorService that facilitate the creation and management of thread pools.

Image description

  1. ๐—ง๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ ๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐—ฟ๐˜‚๐—ฝ๐˜๐—ถ๐—ผ๐—ป:
    • You can request that a thread be interrupted by calling thread.interrupt(). The running thread should periodically check if it has been interrupted using the Thread.interrupted() or isInterrupted() methods.

๐—”๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€ ๐—ฎ๐—ป๐—ฑ ๐——๐—ถ๐˜€๐—ฎ๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€

  • ๐—”๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€:

    • ๐—•๐—ฒ๐˜๐˜๐—ฒ๐—ฟ ๐˜‚๐˜€๐—ฒ ๐—ผ๐—ณ ๐˜€๐˜†๐˜€๐˜๐—ฒ๐—บ ๐—ฟ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€: It can increase efficiency by utilizing multiple CPU cores.
    • ๐—œ๐—บ๐—ฝ๐—ฟ๐—ผ๐˜ƒ๐—ฒ๐—ฑ ๐—œ/๐—ข ๐—ฝ๐—ฒ๐—ฟ๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐—ป๐—ฐ๐—ฒ: Threads can continue executing while an I/O operation is waiting.
  • ๐——๐—ถ๐˜€๐—ฎ๐—ฑ๐˜ƒ๐—ฎ๐—ป๐˜๐—ฎ๐—ด๐—ฒ๐˜€:

    • ๐—–๐—ผ๐—บ๐—ฝ๐—น๐—ฒ๐˜…๐—ถ๐˜๐˜†: Managing multiple threads can be complicated, especially when it comes to synchronization and communication.

    - ๐—ฆ๐˜†๐˜€๐˜๐—ฒ๐—บ ๐—ผ๐˜ƒ๐—ฒ๐—ฟ๐—ต๐—ฒ๐—ฎ๐—ฑ: Creating and managing threads adds overhead to the system.

Image description๐—ฅ๐—ฎ๐—ฐ๐—ฒ ๐—ฐ๐—ผ๐—ป๐—ฑ๐—ถ๐˜๐—ถ๐—ผ๐—ป๐˜€ ๐—ฎ๐—ป๐—ฑ ๐—ฑ๐—ฒ๐—ฎ๐—ฑ๐—น๐—ผ๐—ฐ๐—ธ๐˜€: These are common issues in multi-threaded applications, requiring extra care in system design.

Threads are a powerful tool in Java for developing applications that require simultaneous task execution. However, improper use can lead to complex problems such as race conditions and deadlocks, necessitating a careful understanding of their implementation and management.

Top comments (0)