First we would like to thank authors by producing such an excellent book and we recommend to our readers as well.
Below are the table of Contents.
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055)
Perface
Introduction
Chapter 1 - Declarations and Access Control
Chapter 2 - Object Orientation
Chapter 3 - Assignments
Chapter 4 - Operators
Chapter 5 - Flow Control, Exceptions, and Assertions
Chapter 6 - Strings, I/O, Formatting, and Parsing
Chapter 7 - Generics and Collections
Chapter 8 - Inner Classes
Chapter 9 - Threads
Chapter 10 - Development
Appendix A - About the CD
I think all chapters are worth to read but we enjoyed reading Threads and Generics and Collections chapter at the most as frankly it was good learning.
For Example In Threads chapter we learned few new things like....
a) Below program will throw java.lang.IllegalThreadStateException exception as we cannot start obviously a dead thread again.Remember thread state will move to Dead State From Running State when the run() method execution completes.
class MyThread extends Thread {
public static void main(String [] args) {
MyThread t = new MyThread();
t.start() ;
System.out.print("one. ");
t.start();
System.out.print("two. ");
}
public void run() {
System.out.print("Thread ");
}
}
b) In Certification Objective "Thread Interaction" it states that
wait(), notify(), and notifyAll() must be called from within a synchronized context! A thread can't invoke a wait or notify method on an object unless it owns that object's lock.
The methods wait() and notify(), remember, are instance methods of Object. In the same way that every object has a lock, every object can have a list of threads that are waiting for a signal (a notification) from the object. A thread gets on this waiting list by executing the wait() method of the target object. From that moment, it doesn't execute any further instructions until the notify() method of the target object is called. If many threads are waiting on the same object, only one will be chosen (in no guaranteed order) to proceed with its execution. If there are no threads waiting, then no particular action is taken. Let's take a look at some real code that shows one object waiting for another object to notify it (take note, it is somewhat complex):
Note that if the thread calling wait() does not own the lock, it will throw an IllegalMonitorStateException.
public static synchronized void main(String[] args) throws InterruptedException {
Thread t = new Thread();
t.start () ;
System.out.print ("X") ;
t.wait (10000) ;
System.out.print("Y");
}
The code does not acquire a lock on t before calling t.wait(), so it throws an IllegalThreadStateException. The method is synchronized, but it's not synchronized on t so the exception will be thrown. If the wait were placed inside a synchronized(t) block, then it prints XY with a 10-second delay between X and Y.
c) Can you predict the Output of this below program?
public class TwoThreads {
static Thread laurel, hardy;
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
System.out.println("A");
try {
hardy.sleep (1000);
} catch (Exception e) {
System.out.println("B");
}
System.out.println("C");
}
};
hardy = new Thread(} {
public void run() {
System.out.println("D");
try {
laurel.wait();
} catch (Exception e) {
System.out.println("E");
}
System.out.println("F");
}
};
laurel.start();
hardy.start();
}
}
Output of the program will be
A
D
E
F
C
This may look like laurel and hardy are battling to cause the other to sleep() or wait()—but that's not the case. Since sleep() is a static method, it affects the current thread, which is laurel (even though the method is invoked using a reference to hardy). That's misleading but perfectly legal, and the Thread laurel is able to sleep with no exception, printing A and c (after a 1-second delay). Meanwhile hardy tries to call laurel.wait()—but hardy has not synchronized on laurel, so calling laurel.wait() immediately causes an IllegalMonitorStateException, and so hardy prints D, E, and F.Although the order of the output is somewhat indeterminate (we have no way of knowing whether A is printed before D, for example) it is guaranteed that A, C, D, E, and F will all be printed in some order.
Please do read this book even if you are planning to write certification or not as they are loads of learning guaranteed.
6 comments:
The last section of your post states that calling laurel.wait() results in an IllegalThreadStateException being thrown, should it not be IllegalMonitorStateException instead since hardy has not synchronized on laurel?
The last section of your post states that calling laurel.wait() results in an IllegalThreadStateException being thrown, should it not be IllegalMonitorStateException instead since hardy has not synchronized on laurel?
Anonymous,
You are right.It should throw IllegalMonitorStateException only.
I have corrected the mistake.
Thanks
Prashant
Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Fragmentadora de Papel, I hope you enjoy. The address is http://fragmentadora-de-papel.blogspot.com. A hug.
Hi
It is a great and nice post and I like it.
thanks shopping cart.
Post a Comment