Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/synchronization-in-java-nikhil-kumar
Question 245 / 297:  Synchronization issue
class SyncDemo{

public static void main(String... args){

class A implements Runnable{

public synchronized void run(){

display();

}

synchronized void display(){

for(int i=0;i<5;i++){

System.out.print("Hello");

try{

Thread.sleep(2000);

} catch(InterruptedException e){}

System.out.println(Thread.currentThread().getName());

}

}

}

A ob1=new A();

A ob2=new A();

Thread ob3=new Thread(ob1,"Saurabh");

Thread ob4=new Thread(ob2,"Nikhil");

ob3.start();

ob4.start();

}

}

A  Compilation Fails
B  Hello Saurabh Hello Nikhil ...... Printed in Synchronized way
C  Output is Unpredictable .
<< First < Previous Next > Last >>
Exam Home Page
https://www.jobilize.com/java-certification-questions

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Anonymous User
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>