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(); } }
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();
Oracle Certified Professional Java Programmer
Access: Public Instant Grading
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use.