Mukesh, that was a nice summary on threads. There is one thing I want to bring up.
<Q>I/O operation � if an executing thread calls any method which requires some input stream to be responded, then the thread waits for such input and hence is blocked. Any such blocking of thread will ensures that it releases the lock.</Q>
Is that true? I think blocking for I/O does not release any locks that are held. Try the following code for example.
<CODE>
import java.io.*;
public class r implements Runnable {
static byte[] b = new byte[0];
String name;
r(String name) { this.name = name; }
public void run() {
synchronized (b) {
for (int count=0; count<10; count++) System.out.println(name + count);
int i = 0;
try { while (i!=-1) {
i = System.in.read();
System.out.println(name + i);
}
} catch(IOException ioe) { System.out.println(name + "IOException"); }
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new r("first thread - "));
t1.start();
Thread t2 = new Thread(new r("second thread - "));
t2.start();
}
}
</CODE>
When you run this code, the first thread prints 0 to 9 and then blocks for I/O. But it does not relinquish the lock. If it did, the second thread would have printed 0 to 9 immediately after the first thread prints 9. But in fact, the second thread prints 0 to 9 only after the first thread completes I/O.
Note:- Press the end of file indicator (CTRL-Z on PCs) to stop reading from System.in
Do all agree ?