Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Thread

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

**Thread safe:

Only 1 thread can execute a block of code at a time.

***
We can only start a thread once.
Means can call thread.start(), only once.
If we call it again, we will get illigalThreadStateException.

****
Directly call run method:
If you call run method directly , it won't create a new thread and it will be in
same stack as main.

*****Sleep Vs Yield::

Sleep::In java if a thread doesn’t want to perform any operation for a particular
amount of time then we should go for the sleep() method, which causes the currently
executing thread to stop for the specified number of milliseconds.

Yield::It causes to pause the currently executing thread to give the chance for
waiting thread of same priority. If there are no waiting threads or all waiting
threads have low priority then the same thread can continue its execution. If
multiples thread is waiting with same priority then which waiting thread will get
the chance we can’t say it depends on the thread scheduler. The thread which is
yield when it will get the chance once again also depends on the thread scheduler.

Wait::
wait() method is a part of java.lang.Object class. When wait() method is called,
the calling thread stops its execution until notify() or notifyAll() method is
invoked by some other Thread.

join::

java.lang.Thread class provides the join() method which allows one thread to wait
until another thread completes its execution. If t is a Thread object whose thread
is currently executing, then t.join() will make sure that t is terminated before
the next instruction is executed by the program.

Daemon::

Daemon thread in Java is a low-priority thread that runs in the background to


perform tasks such as garbage collection. Daemon thread in Java is also a service
provider thread that provides services to the user thread. Its life depends on the
mercy of user threads i.e. when all the user threads die, JVM terminates this
thread automatically.

In simple words, we can say that it provides services to user threads for
background supporting tasks. It has no role in life other than to serve user
threads.

****

Difference between wait() and join() method

Most obvious difference, both are present different packages, the wait() method is
declared in java.lang.Object class while join() is declared in java.lang.Thread
class.
The wait() is used for inter-thread communication while the join() is used for
adding sequencing between multiple threads, one thread starts execution after first
thread execution finished.
We can start a waiting thread (went into this state by calling wait()) by using
notify() and notifyAll() method but we can not break the waiting imposed by join
without unless or interruption the thread on which join is called has execution
finished.
One most important difference between wait() and join() that is wait() must be
called from synchronized context i.e. synchronized block or method otherwise it
will throw IllegalMonitorStateException but On the other hand, we can call join()
method with and without synchronized context in Java.
Let us understand the differences in a tab

Java Thread Pool::

Java Thread pool represents a group of worker threads that are waiting for the job
and reused many times.

In the case of a thread pool, a group of fixed-size threads is created. A thread


from the thread pool is pulled out and assigned a job by the service provider.
After completion of the job, the thread is contained in the thread pool again.

** Life cycle:

New, Runnable/Running, Blocked, Waiting, Timed Waiting, Terminated.

A NEW Thread (or a Born Thread) is a thread that's been created but not yet
started.

Threads in this state are either running or ready to run, but they're waiting for
resource allocation from the system.

It enters this state when it is waiting for a monitor lock and is trying to access
a section of code that is locked by some other thread.(t1 is working on synchronzed
block, so t2 is blocked).

A thread is in WAITING state when it's waiting for some other thread to perform a
particular action.(by using wait, join).

A thread is in TIMED_WAITING state when it's waiting for another thread to perform
a particular action within a stipulated amount of time.(sleep(100), wait(100)).

It's in the TERMINATED state when it has either finished execution or was
terminated abnormally.

You might also like