If you are a newbie to working with java threads, this post will help you. Certifications like the SCJP require you to understand java threads to a fair degree. Threads behavior can be difficult to understand even for experienced programmers, so I will try to present some examples which will help candidates identify how threads wait / lock and synchronize.
Lets cut the chit chat and jump into a problem. A program increments a counter in a for loop as shown below.
Synchronizing:
public class StaticSync
{
public static final Object s_lock = new Object();
public static int s_counter=0;
public static void main(String... args)
{
new StaticSync().go();
}
public void go()
{
for(int iCounter=0; iCounter<50; iCounter++)
{
Thread thread = new Thread(new Incrementer(new StaticSync()));
thread.start();
}
}
}
class Incrementer implements Runnable
{
StaticSync m_staticSync=null;
public Incrementer(StaticSync staticSync_INP)
{
m_staticSync = staticSync_INP;
}
public void run()
{
synchronized (m_staticSync.s_lock)
{
m_staticSync.s_counter++;
System.out.println(m_staticSync.s_counter);
}
}
}
This program is pretty simple. This is the sort of code that you can expect on the SCJP exam. Advanced versions of the code above can also appear by adding notify() wait() sleep() etc into the picture. Let us concentrate on the program for now. What can be guaranteed about the output of this program ?
To get sensitive user input without echoing it to the console/terminal, the Java SDK has introduced the java.io.Console class. The Console class is quite unique in the way it handles its data. It uses the native encoding of the system instead of the using the JVM’s default encoding.
The Console class provides methods to access the console/terminal, if any is associated with the JVM.
Using the Console class:
So how do you use the Console class ? Its pretty simple really. First get a reference to the Console class from the System class
Console con = System.console();
It is not always possible to get a reference to the console. Some scenarios prevent the developer from gaining access to the console. This method may return null in these cases. For example a batch process running on a server will not expect user input and usually does have a console. Your code must be capable of handling such situations.
Before you can go ahead and take the real Sun Java exams, you need to know some of the pitfalls that have plagued several test takers. Here are a few pointer
Do not buy a voucher before you are prepared to take the exam:
Often some candidates like to follow this approach hoping that the purchase of the voucher would force them to learn within a given time period. On the contrary this adds additional pressure to your preparation, since you need to study the objectives before the expiry date. Some candidates have lost money misinterpreting the expiry date on the voucher. Be wary of the MM/DD/YYYY Vs DD/MM/YYYY formats. Which one does your voucher display ?
When the candidates finally find that they have no more time to learn, they post a message on a forum that reads something like ‘I have voucher for exam X for a discount of Y%. Please call me to buy’. Remember, you are a candidate, not a voucher reseller
Sets:
A Set is a collection that contains no duplicate elements. Sets are collections where no two elements are the same, which means that element1.equals(element2) should never return true. Also at most one element in a set can be null and no more.
A Set. Duplicates are not allowed and at most one null element is allowed

Sorted Sets:
A SortedSet is a set which sorts and maintains the ordering of its elements. Elements will be ordered by their natural ordering or by the use of a Comparator (if the developer provides one). A Comparable interface can help define the ordering for elements. For example a String’s natural ordering will dictate that elements be ordered this way “a”, “b”, “c” etc. The way in which elements are ordered in a Sorted data structure can be redefined by using the Comparator interface.