Thread: A thread is defined at the operating system level. It is a single execution of a program.
A thread is a set of instruction. An application can be composed of several threads. Different threads can be executed at the same time. The JVM works with several threads like Garbage collector, JIT etc.
What does mean by "At the same time"?
Race Condition: Accessing data concurrently might lead to the issue. It means two different threads are trying to read and write the same variable at the same time called Race Condition.
How to prevent this?
To prevent this situation which causes issues can be prevented with synchronization. Synchronization prevents a block of code and ensure that it can only be executed by a single thread at a time.
To achieve synchronization, there is a concept called lock which is achieved using key. So, for synchronization to work we need a special technical object that will hold the key. In fact, every java object can play this role. It is also called monitor.Every object has a key.
How do you know which object is used for synchronization?
Thus to execute a task by thread happen in below steps -
A common mistake and important point to note here :
A thread is a set of instruction. An application can be composed of several threads. Different threads can be executed at the same time. The JVM works with several threads like Garbage collector, JIT etc.
What does mean by "At the same time"?
- In a single core of CPU, the threads cannot works at the same time, it give us a feel that it is running at same but in reality it work one by one and the context switch happen very fast in milliseconds which we can't observe.
- On a multi-core CPU, things happens at the same time. A challenge while this is the read and write operation involve while running different thread at same time.
Who is responsible for the CPU sharing?
- There is a special element who does this, is called scheduler.there are three reasons for a scheduled to pause a thread -
- The CPU should be shared equally among threads.
- The thread might be waiting for some data .
- The thread might be waiting for another thread to do something.
Race Condition: Accessing data concurrently might lead to the issue. It means two different threads are trying to read and write the same variable at the same time called Race Condition.
How to prevent this?
To prevent this situation which causes issues can be prevented with synchronization. Synchronization prevents a block of code and ensure that it can only be executed by a single thread at a time.
To achieve synchronization, there is a concept called lock which is achieved using key. So, for synchronization to work we need a special technical object that will hold the key. In fact, every java object can play this role. It is also called monitor.Every object has a key.
How do you know which object is used for synchronization?
- For static synchronized method, class itself is used as synchronization object and used to hold the key.
- For non-static synchronized method, instance is used as synchronization object, used to hold the key.
- We can also use a dedicated object to synchronized.
Deadlock: A deadlock is a situation where a thread T1 holds a key needed by a thread T2 and T2 holds the key needed by T1. Thus both keeps waiting infinite for one another key which is called deadlock condition.
Runnable Pattern: The most basic way to create thread in java is using Runnable pattern. We can create a task using Runnable interface implementation.
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.
In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class
Runnable Pattern: The most basic way to create thread in java is using Runnable pattern. We can create a task using Runnable interface implementation.
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.
In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class
Thus to execute a task by thread happen in below steps -
- Create an instance of Runnable
- Create an instance of thread with task as a parameter
- Launch the thread by calling start() mentod on the instance of thread
A common mistake and important point to note here :
- Do not call the run() method instead of the start() method.
- Thread.currentThread() static method returns the current thread
How to stop thread?
It is more tricky. There is a method in the Thread class called stop() should not be called, it is also deprecated and not removed to support the code developed with older version of java. It is just there for legacy and backward compatibility reasons. The right pattern is to use the interrupt() method.
The code of the task should call isInterurupted() to terminate itself. If the thread is blocked or waiting, it will throw InterruptedException.
Comments
Post a Comment