aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Sangita Mishra
Greenhorn

Joined: Jun 15, 2005
Posts: 22
Can anybody explain the flow of this program?

class A1 extends Thread {
String[] sa;
public A1(String[] sa) {this.sa = sa;}
public void run() {
synchronized (sa) {
while (!sa[0].equals("Done")) {
try {sa.wait();} catch (InterruptedException ie) {}
}}
System.out.print(sa[1] + sa[2] + sa[3]);
}}
class B11 {
private static String[] sa = new String[]{"Not Done","X","Y","Z"};
public static void main (String[] args) {
Thread t1 = new A1(sa); t1.start();
synchronized (sa) {
sa[0] = "Done";
sa[1] = "A"; sa[2] = "B"; sa[3] = "C";
sa.notify();
}}}

Output: ABC
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hint: Consider the line t1.start(); in the main method of B11. What does this do? Will the instance of A1 run? If so, what will it do? If not, what will happen?

Reformatted with code tags...

[ June 20, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
Thread!
Thread - Wait Doubt
Synchronized Objects
Thread Synchronization??
Dan's: Thread Q 39