*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need Help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need Help?" Watch "Need Help?" New topic
Author

Need Help?

Jaffery Rab
Ranch Hand

Joined: Jan 18, 2002
Posts: 36
hi
guys I need ur help
class Foo{
static final StringBuffer sb1=new StringBuffer();
static final StringBuffer sb2=new StringBuffer();
public static void main(String args[]) {
new Thread(){
public void run(){
synchronized(sb1){
sb1.append("A");
sb2.append("B");
}
}
}.start();
new Thread(){
public void run(){
synchronized(sb1){
sb1.append("C");
sb2.append("D");
}
}
}.start();
System.out.println (sb1+" "+sb2); }
when I comppile this out put is nothing but I have options like
A)main will finish before starting threads
B)main will finish middle of one thread
C) main will finish after one thread
D)main will finish in middle of the second thread
E)main will finish after the second thread
which one is correct and offcourse why???
Thanx
Jaffery
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
The answer is "It is impossible to predict."
Sometimes the main thread will finish before either of the other two threads and then you will see no output because both StringBuffers are initialized to no output.
Try this:

But the main thread may finish after the first new Thread, and the main thread may finish after both new Threads.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jaffery Rab
Ranch Hand

Joined: Jan 18, 2002
Posts: 36
Thanx
I appreciate..
Jaffery
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help?
 
Similar Threads
Doubt regarding StringBuffer
Quession obout synchronized
A deadlock thread question.
how and why to do it ?
Threads and Deadlock - a question in SCJP exam