thread - ik21191/core-java GitHub Wiki

Thread: A program can be divided into a number of small processes. Each small process can be addressed as a single thread (a lightweight process). You can think of a lightweight process as a virtual CPU that executes code or system calls. You usually do not need to concern yourself with lightweight processes to program with threads. Multithreaded programs contain two or more threads that can run concurrently and each thread defines a separate path of execution. This means that a single program can perform two or more tasks simultaneously. For example, one thread is writing content on a file at the same time another thread is performing spelling check.

In Java, the word thread means two different things.

An instance of Thread class. or, A thread of execution. An instance of Thread class is just an object, like any other object in java. But a thread of execution means an individual "lightweight" process that has its own call stack. In java each thread has its own call stack.

thread-call-stack

https://www.wideskills.com/java-tutorial/java-threads-tutorial

Synchronization: https://www.mygreatlearning.com/blog/synchronization-in-java/

Object and Class level lock: https://www.tutorialspoint.com/object-level-lock-vs-class-level-lock-in-java

Thread Communication

Join : https://www.edureka.co/blog/join-method-java


Reference : https://www.studytonight.com/java/interthread-communication

Wait and Notify : https://www.baeldung.com/java-wait-notify

https://www.geeksforgeeks.org/inter-thread-communication-java/

Reason of not calling wait/notify in synchronized method: https://stackoverflow.com/questions/25295756/why-to-use-wait-method-on-strings-or-other-objects

Spurious wakeup : http://tutorials.jenkov.com/java-concurrency/thread-signaling.html

Thread Priority : https://www.geeksforgeeks.org/java-thread-priority-multithreading/#:~:text=Whenever%20we%20create%20a%20thread,in%20Thread%20class%20for%20priority.

Wait and Sleep : http://javaconceptoftheday.com/difference-between-wait-and-sleep-methods-in-java/

Thread: Monitor and Lock real example
https://www.programcreek.com/2011/12/monitors-java-synchronization-mechanism/

How to avoid deadlock: One of the best ways to prevent the potential for deadlock is to avoid acquiring more than one lock at a time, which is often practical. However, if that is not possible, you need a strategy that ensures you acquire multiple locks in a consistent, defined order.

https://howtodoinjava.com/core-java/multi-threading/writing-a-deadlock-and-resolving-in-java/

Thread Interruption: If any thread is in sleeping or waiting state then using interrupt() method, we can interrupt the execution of that thread by showing InterruptedException. A thread which is in the sleeping or waiting state can be interrupted with the help of interrupt() method of Thread class.

Reference: https://www.geeksforgeeks.org/how-a-thread-can-interrupt-an-another-thread-in-java/

ThreadLocal: http://tutorials.jenkov.com/java-concurrency/threadlocal.html

Volatile Keyword:

https://www.scientecheasy.com/2020/08/volatile-keyword-in-java.html/

⚠️ **GitHub.com Fallback** ⚠️