Java Thread Programming FAQ - 1

Thursday, January 29, 2009

Many a times as far as thread programming is concerned, the first thing which comes to our mind is the keyword synchronized (of course the other dangerous word is DEADLOCK!), because one can directly relate the word synchronized to java thread programming or in general to thread programming concepts, the designers of java were intelligent enough to define a new meaning to the keyword synchronized, simplifying the way multiple threads access a shared resource with the synchronized construct.

On one side, java thread programming may seem easy and interesting to learn with lots of excellent tutorials all around the web, but the real challenge comes when you use those concepts in your applications, many books have been written to cover the length and breadth of java thread programming in itself, when I was doing my SCJP 1.6, I found it very easy to learn and grasp the thread programming concepts from books, but when it comes to reading/writing code related to java threads and identifying hard to catch bugs, those learning skills differed from practice, which straightaway motivated me to write a FAQ series for java thread programming for my reference as well as yours.

I am writing this FAQ based on my experiences on java thread programming learning from mistakes, some of these when asked to yourself will bring more clarity to your understanding of java thread concepts. Lets start with some funny questions and then dig deep into it.

1. Why its a fun programming java threads?

Because there is no fun writing a java code without using threads in a real time application, also a programmer's understanding of java is not complete without a deep knowledge of threads, java game applications use threads, java stock ticker programs use threads, java server applications uses threads, J2ME applications use threads, so its always a fun to bring your own creativity in using threads wisely which would make a difference to thousands of end users.

2. Why its not so fun programming java threads?

Because you can not only kill threads in java but can kill the application as well with wrong usage of threads and there's always a DEADLOCK to add to the nightmare and its critical to ensure that your application is not a resource hog when it comes to fairplay when it's waiting for I/O, relinquishing control at the right time.

3. What are the methods of the Thread class and the Object class?

The following are some important methods of Thread class.

join()
sleep()
yield()
start()
run() - This is a method in Runnable interface but used here to distinguish between Thread and Object class methods in general (in the sense that it has to be implemented or overridden anyway by a thread).
interrupt()

The following are the important methods of Object class which are used by threads

wait()
notify()
notifyall()

4. What are the static and instance methods in a Thread class?

The following are the static methods in Thread class which apply to the currently running thread.

yield()
sleep()

The other methods that are specific to the Thread object are

start()
run()
join()
interrupt()

This is important to understand, the static and object methods in threads, there is a logic behind the static methods in the sense that the threads which voluntarily relinquish control and give a chance for other threads to run (fair play) would call the appropriate methods from their static contexts (when its a live thread running) and these methods should not be called on other thread objects, say an application thread A should not have the privilege to forcibly preempt another running thread, the operating system scheduler is there to take care of that.

5. What are the methods which are be called from synchronized context?

The following methods are called from synchronized context

All Object class methods which a thread may use as described above should be called from a synchronized context, like

wait()
notify()
notifyall()

The thread invoking these methods should have a lock on the object on which it calls these methods, an ideal example is a Producer-Consumer problem where a consumer thread waits for the producer to produce some data for reading(consuming), once its done, the producer notifies the consumer so that the consumer may read the data, the notifyall() method will notify multiple waiting threads, say for example a scenario where multiple reader threads reading the latest stock quote waits for the lock on a thread which gets the current stock quote and notifies them.

6. What are the thread methods which throws an exception which should be handled in a try/catch block?

join()
sleep()

The join() method joins after another thread completes or in other words stops execution and continue after the the thread on which it calls join completes, the sleep method goes to sleep state relinquishing control, both these calls can be interrupted by another thread so that they can come out from the wait/sleep state.

In addition, there are also other deprecated thread methods like stop, suspend, resume which throws SecurityException.

Ok, thats it for this post, we will see more code examples and other thread concepts in a future FAQ column.

No comments:


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.