aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads(Can someone please explain why the answer to the following questions are unpredictable) 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 "Threads(Can someone please explain why the answer to the following questions are unpredictable)" Watch "Threads(Can someone please explain why the answer to the following questions are unpredictable)" New topic
Author

Threads(Can someone please explain why the answer to the following questions are unpredictable)

Moses Marfo
Greenhorn

Joined: May 24, 2009
Posts: 27
Source. Examlab (Practice 2, ques 29 and 48).

What is the result of compiling and running the two program below
1.
class HighTop extends Thread{

public void run(){
staticMethod();
}

public static synchronized void staticMethod(){
for(int i=0;i<5;i++){
System.out.print(" A"+i);
}
}

}


public class LowTop extends HighTop{

public void run(){
staticMethod();
}

public static synchronized void staticMethod(){
for(int i=0;i<5;i++){
System.out.print(" B"+i);
}
}

public static void main(String args[]){
HighTop t1=new HighTop();
LowTop t2=new LowTop();
t2.start();
t1.start();
}

}

Among the answers to choose from are:
A. Prints B0 B1 B2 B3 B4 A0 A1 A2 A3 A4 or A0 A1 A2 A3 A4 B0 B1 B2 B3 B4
B. unpredictable

I'm thinking it should be A.

2. Below is a similar question.
class TheA extends Thread{
public void run(){
synchronized(this){
for(int i=0; i<10; i++){
System.out.print(i+",");
}
}
}
}

class TheB extends TheA{
public void run(){
synchronized(this){
for(int i=0; i<10; i++){
System.out.print(i+",");
}
}
}
}

public class TDemo{
public static void main(String argv[]){
TheA ta=new TheA();
TheB tb=new TheB();
ta.start();
tb.start();
}
}

Among the answers to choose from are:
A.unpredictable
B.prints 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,

I'm thinking the answer should be B.

thanks in advance for the help.


Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Both programs have two threads running concurrently. The two threads try to acquire locks on different objects. No reason for them to wait.


cheers
Bob


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Moses Marfo
Greenhorn

Joined: May 24, 2009
Posts: 27
Bob Wheeler wrote:Both programs have two threads running concurrently. The two threads try to acquire locks on different objects. No reason for them to wait.


cheers
Bob


Thanks Bob, wasn't thinking
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Moses, welcome to javaranch

When you post a source code, then Use Code Tags to make the program readable like the following



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Moses Marfo
Greenhorn

Joined: May 24, 2009
Posts: 27
Moses Marfo wrote:Source. Examlab (Practice 2, ques 29 and 48).

What is the result of compiling and running the two program below
1.

Among the answers to choose from are:
A. Prints B0 B1 B2 B3 B4 A0 A1 A2 A3 A4 or A0 A1 A2 A3 A4 B0 B1 B2 B3 B4
B. unpredictable

I'm thinking it should be A.

2. Below is a similar question.

Among the answers to choose from are:
A.unpredictable
B.prints 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,

I'm thinking the answer should be B.

thanks in advance for the help.


ambika mullasseri
Greenhorn

Joined: Jun 01, 2009
Posts: 28
Hi

can sombody explain this in detail please.


SCJP 6.0 86%
Jim Toy
Greenhorn

Joined: Oct 17, 2002
Posts: 14
Wouldn't the answer be "unpredictable" since the output of either thread can not be guaranteed? Even though there is no contention, the thread pool scheduler will run/stop/run then in its own sweet time.
mansi gupte
Ranch Hand

Joined: Dec 30, 2008
Posts: 72
Jim Toy wrote:Wouldn't the answer be "unpredictable" since the output of either thread can not be guaranteed? Even though there is no contention, the thread pool scheduler will run/stop/run then in its own sweet time.



Will the output still be unpredictable , even if the method is synchronized?

Please explain someone??
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Jim Toy wrote:Wouldn't the answer be "unpredictable" since the output of either thread can not be guaranteed? Even though there is no contention, the thread pool scheduler will run/stop/run then in its own sweet time.

I'm a little bit confused, because there are two codes around. The difference between the two codes are the locks which the threads hold. In the first code it is a lock on a class (e.g. YourClass.name), in the second code it is the thread instance (e.g. yourThread). But the two threads don't try to acquire the same lock. Therefore, we have concurrently threading.
Is the outcome unpredictable? Sort of, but I don't know the other answers of the assignment.

cheers
Bob
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

piya rai wrote:
Will the output still be unpredictable , even if the method is synchronized?

Please explain someone??


If you see, the methods are synchronized. Which method's synchronization are you talking about exactly?? The two static methods called by the run method are synchronized.

Jim Toy wrote:Wouldn't the answer be "unpredictable" since the output of either thread can not be guaranteed?


The correct answer includes an option which says the output is unpredictable. So what are you trying to say.

Well in both the cases, the locks are required on different objects so both the threads won't block each other. In the first example, the run methods of the two threads call a static method. Both the static methods are synchronized but they are in different classes. So both will need a different lock. staticMethod() in HighTop would need a lock on HighTop.class while staticMethod() in LowTop will need a lock on LowTop.class. So they won't block each other.

In the second example, the two threads are started with different objects and they both lock on this. So if we take a look at the code



run in both the threads will lock on different objects that were created inside the main method...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads(Can someone please explain why the answer to the following questions are unpredictable)