{{theTime}}

Search This Blog

Total Pageviews

Java Thread States: New, Runnable, Running, Dead, Waiting/blocked/sleeping

Thread States
A thread can be only in one of five states (see Figure 9-2):

New
'This is the state the thread is in after the Thread instance has been created, but the start() method has not been invoked on the thread. It is a live Thread object, but not yet a thread of execution. At this point, the thread is considered not alive.

Runnable This is the state a thread is in when it's eligible to run, but the scheduler has not selected it to be the running thread. A thread first enters the runnable state when the start() method is invoked, but a thread can also return to the runnable state after either running or coming back from a blocked, waiting, or sleeping state. When the thread is in the runnable state, it is considered alive.

Running
This is it. The "big time." Where the action is. This is the state a thread is in when the thread scheduler selects it (from the runnable pool) to be the currently executing process. A thread can transition out of a running state for several reasons, including because "the thread scheduler felt like it." We'll look at those other reasons shortly. Note that in Figure 9-2, there are several ways to get to the runnable state, but only one way to get to the running state: the scheduler chooses a thread from the runnable pool.

Waiting/blocked/sleeping This is the state a thread is in when it's eligible to run. Okay, so this is really three states combined into one, but they all have one thing in common: the thread is still alive, but is currently not eligible to run. In other words, it is not runnable, but it might return to a runnable state later if a particular event occurs. A thread may be blocked waiting for a resource (like I/O or an object's lock), in which case the event that sends it back to runnable is the availability of the resource—for example, if data comes in through the input stream the thread code is reading from, or if the object's lock suddenly becomes available. A thread may be sleeping because the thread's run code tells it to sleep for some period of time, in which case the event that sends it back to runnable is that it wakes up because its sleep time has expired. Or the thread may be waiting, because the thread's run code causes it to wait, in which case the event that sends it back to runnable is that another thread sends a notification that it may no longer be necessary for the thread to wait. The important point is that one thread does not tell another thread to block. Some methods may look like they tell another thread to block, but they don't. If you have a reference t to another thread, you can write something like this:

t.sleep(); or t.yield()

But those are actually static methods of the Thread class—they don't affect the instance t; instead they are defined to always affect the thread that's currently executing. (This is a good example of why it's a bad idea to use an instance variable to access a static method—it's misleading. There is a method, suspend(), in the Thread class, that lets one thread tell another to suspend, but the suspend() method has been, deprecated and won't be on the exam (nor will its counterpart resume()). There is also a stop () method, but it too has been deprecated and we won't even go there. Both suspend () and stop() turned out to be very dangerous, so you shouldn't use them and again, because they're deprecated, they won't appear on the exam. Don't study 'em, don't use 'em. Note also that a thread in a blocked state is still considered to be alive.

Dead
A thread is considered dead when its run() method completes. It may still be a viable Thread object, but it is no longer a separate thread of execution. Once a thread is dead, it can never be brought back to life! (The whole "I see dead threads" thing.) If you invoke start() on a dead Thread instance, you'll get a runtime (not compiler) exception. And it probably doesn't take a rocket scientist to tell you that if a thread is dead, it is no longer considered to be alive.

14 comments:

maze said...

Informative blog. Now I Clearly understand the thread concepts. thanks for sharing this blog with us.This information would benefit anyone preparing for the exams.

Mounika said...

Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me.
python Training institute in Pune
python Training institute in Chennai
python Training institute in Bangalore

Sadhana Rathore said...

Very good blog with lots of information, keep up the good work and share more like this.
Python Training in Chennai
Python Training Institute in Chennai
Data Science course in Chennai
Data Science Training in Chennai
ccna course in Chennai
ccna Training institute in Chennai
Python Training in OMR
Python Training in Porur

Unknown said...

Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
Best Devops online Training
Online DevOps Certification Course - Gangboard
Best Devops Training institute in Chennai

Anbarasan14 said...

Thanks to the admin for sharing this blog with us. The info in this blog was really helpful to me.
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
IELTS Training in Chennai
Best IELTS Coaching in Chennai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
Best IELTS Coaching in Mumbai
IELTS Center in Mumbai

SriwebEo-Coimbatore said...

Training in Coimbatore provides SEO training, Web Designing Training with real-time working professional which will help students and trainees to get trained in practical real-time .
Job Placement Training With 100% Assitance Support to be an expert at all subjects.
Web Design Training

Kasaikannu Catering said...

Best Catering Services in Chennai | Best Catering Service in Chennai | Best Catering Services Chennai | Best Catering Service Chennai | Best Catering Services | Best Catering Service

unknown said...

Informative post indeed, I’ve being in and out reading posts regularly and I see alot of engaging people sharing things and majority of the shared information is very valuable and so, here’s my fine read.
click here bad practice
click here button css
click here button ux
click here button instagram story
click here clipart

deiva said...

Thanks to the admin for sharing this blog with us. The info in this blog was really helpful to me.
web designing training in chennai

web designing training in omr

digital marketing training in chennai

digital marketing training in omr

rpa training in chennai

rpa training in omr

tally training in chennai

tally training in omr

Pathway for German Language said...
This comment has been removed by the author.
vé máy bay từ Nhật Bản về Việt Nam said...

Đặt vé máy bay tại Aivivu, tham khảo

mua ve may bay tu han quoc ve viet nam

vé máy bay pacific airlines hải phòng sài gòn

vé máy bay từ tphcm ra hà nội

giá vé đi nha trang

vé máy bay đi đà lạt

taxi sân bay nội bài

Kaparthicynixit said...

Nice post, i hope really gather more information here. thanks for sharing your ideas.
Java Training
Visit us: Java Online Training

Pavithra Devi said...

This post is so interactive and informative.keep update more information...
PHP Training in Tambaram
PHP Training in Tambaram

iteducationcentre said...

Thanks for posting such an wonderful and useful post.
it was worth reading.
Java classes in Pune

Generate Insert Sql from Select Statement

SELECT 'INSERT INTO ReferenceTable (ID, Name) VALUES (' +        CAST(ID AS NVARCHAR) + ', ''' + Name + ''...