Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Moses Marfo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Moses Marfo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moses, welcome to javaranch

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

 
Moses Marfo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

can sombody explain this in detail please.
 
Jim Toy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic