Couple Of Days back i started reading Complete Java 2 Certification Study Guide, 5th Edition By Phillip Heller, Simon Roberts.
I wanted to share few important quotations i found from the next 2 chapters(7-8) of the book.
1) To make a thread execute, you call its start() method. Doing so registers the thread with a piece of system code called the thread scheduler. The scheduler might be part of the JVM or of the host operating system. The scheduler determines which thread is running on each available CPU at any given time. Note that calling your thread’s start() method doesn’t immediately cause the thread to run; it just makes the thread
eligible to run. The thread must still contend for CPU time with all the other threads. If all is well, then at some point in the future the thread scheduler will permit your thread to execute.
2) You can’t restart a dead thread by calling its start() or run() methods.
3) Thread States
When you call start() on a thread, the thread does not run immediately. It goes into a readyto-
run state and stays there until the scheduler moves it to the running state. Then the run()
method is called. In the course of executing run(), the thread may temporarily give up the CPU
and enter some other state for a while. It is important to be aware of the possible states a thread
might be in and of the triggers that can cause the thread’s state to change.
The thread states are as follows:
Running A running thread gets the full attention of the JVM’s processor, which executes the
thread’s run() method.
Various non-running states The basic non-running states are Suspended, Sleeping, and Blocked.
Ready A ready thread can enter the Running state as soon as the JVM’s processor is assigned to it.
Dead A dead thread has completed execution of its run() method.
4) Thread Priorities
Every thread has a priority, which is an integer from 1 to 10; threads with higher priority should
get preference over threads with lower priority. The thread scheduler considers the priority when
it decides which ready thread should execute. The scheduler generally chooses the highest-priority
waiting thread. If more than one thread is waiting, the scheduler chooses one of them. There is no
guarantee that the thread chosen will be the one that has been waiting the longest.
The default priority is 5, but all newly created threads have their priority set to that of the creating thread. To set a thread’s priority, call the setPriority() method, passing in the desired
new priority. The getPriority() method returns a thread’s priority. The following code fragment
increments the priority of thread theThread, provided the priority is less than 10. Instead of hard-coding the value 10, the fragment uses the constant MAX_PRIORITY. The Thread class also defines constants for MIN_PRIORITY (which is 1) and NORM_PRIORITY (which is 5).
a. int oldPriority = theThread.getPriority();
b. int newPriority = Math.min(oldPriority+1,
c. Thread.MAX_PRIORITY);
d. theThread.setPriority(newPriority);
5) Daemon Threads
Some threads are daemon threads. Daemon threads are infrastructure threads, created automatically by the JVM. The garbage collector is a daemon thread, and so is the GUI event-processing thread.
When an application begins to run, there is only one non-daemon thread in existence: the main thread, which runs your main() method. Any threads created by daemon threads are initially daemon threads. Threads created by non-daemon threads are initially non-daemon threads. Before a thread begins execution, you can change its daemon status by calling its setDaemon() method,which takes a boolean argument. The JVM runs until the only live threads are daemons. In other words, the JVM considers its work to be done when the only remaining threads are its own infrastructure threads.
6) Yielding
A thread can offer to move out of the virtual CPU by yielding. A call to the yield() method causes the currently executing thread to move to the Ready state if the scheduler is willing to run any other thread in place of the yielding thread. A thread that has yielded goes into the Ready state. There are two possible scenarios. If any other threads are in the Ready state, then the thread that just yielded might have to wait a while before it gets to execute again. However, if no other threads are waiting, then the thread that just yielded will get to continue executing immediately. Note that most schedulers do not stop the yielding thread from running in favor of a thread of lower priority.The yield() method is a static method of the Thread class. It always causes the currently executing thread to yield.
7) Suspending
Suspending a thread is a mechanism that allows any arbitrary thread to make another thread unready for an indefinite period of time. The suspended thread becomes ready when some other thread resumes it. This might feel like a useful technique, but it is very easy to cause deadlock in a program using these methods a thread has no control over when it is suspended (the control comes from outside the thread) and it might be in a critical section, holding an object lock at the time. The exact effect of suspend() and resume() is much better implemented using wait() and notify().
8) The Object Lock and Synchronization
Every object has a lock. At any moment, that lock is controlled by, at most, one single thread. The lock controls access to the object’s synchronized code. A thread that wants to execute an object’s synchronized code must first attempt to acquire that object’s lock. If the lock is available that is, if it is not already controlled by another thread then all is well. If the lock is under another thread’s control, then the attempting thread goes into the Seeking Lock state and becomes ready only when the lock becomes available. When a thread that owns a lock passes out of the synchronized code, the thread automatically gives up the lock. All this lockchecking and state-changing is done behind the scenes; the only explicit programming you need to do is to declare code to be synchronized.
9) wait() and notify()
The wait() and notify() methods provide a way for a shared object to pause a thread when it becomes unavailable to that thread and to allow the thread to continue when appropriate.The threads themselves never have to check the state of the shared object.An object that controls its client threads in this manner is known as a monitor. In strict Java terminology, a monitor is any object that has some synchronized code. Any such object has the infrastructure needed to allow it to block and revive threads. To be really useful, most monitors make use of wait() and notify() methods.
10) Deadlock
The term deadlock describes another class of situations that might prevent a thread from
executing. In general terms, if a thread blocks because it is waiting for a condition, and something
else in the program makes it impossible for that condition to arise, then the thread is
said to be deadlocked
11) A Java thread scheduler can be preemptive or time-sliced, depending on the design of the JVM. No
matter which design is used, a thread becomes eligible for execution (ready) when its start()
method is invoked. When a thread begins execution, the scheduler calls the run() method of the
thread’s target (if there is a target) or the run() method of the thread itself (if there is no target). The target must be an instance of a class that implements the Runnable interface
12) The Object class provides a clone() method, which returns a copy of the current object. In other words, the clone has the same class as the original, and all its data values are identical.Thus all references in the clone point to the same objects as those pointed to in the original.Object’s version of clone() is protected, so a class’ clone() may not be called by any code anywhere. If you want a class’ clone() to be public, you need to insert something like the following:
public Object clone()
throws CloneNotSupportedException {
return super.clone();
}
Notice the CloneNotSupportedException. It is not a runtime exception, so it must be dealt with. Classes that override clone() generally declare that they implement java.lang.Cloneable,which defines the single clone() method
13) Strings
Java uses the String, StringBuffer, and StringBuilder classes to encapsulate strings of characters.Java uses 16-bit Unicode characters to support a broader range of international alphabets than would be possible with traditional 8-bit characters. Both strings and string buffers contain sequences of 16-bit Unicode characters.
About the Authors
Philip Heller is a technical author, novelist, public speaker, and consultant. He has been instrumental in the creation and maintenance of the Java Programmer and Developer exams. His popular seminars on certification have been delivered internationally. He is also the author of Ground-Up Java (available from Sybex), which uses interactive animated illustrations to present fundamental concepts of Java programming to new programmers.
Simon Roberts worked for Sun Microsystems for nine years as an instructor, an authority on the Java language, and the key player in the development of the entire Java certification program. He is now a consultant and instructor, specializing in Java and security. He is also a flight instructor.
Tuesday, 23 September 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment