• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Devaks's PracticeExam1 Q:44 and Q72

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI

This Question is from Devaka's Practice Exam1 Q.No:72 . Output is "ABC" .Agreed. I read the long discussion on this question. . Here Synchronization wont work as it is on different objects. Okay. Please check the MThread class(below XMap class) from PracticeExam-1 Q NO 44 which was giving compiler error but now modified....

OUTPUT : ABC






OUTPUT: BA (is coming)

I felt XMap and MThread are similar. only difference is that XMap implements Runnable and synchronization blocks are there(but synchronization wont work as it is on different objects) but MThread extends Thread And no synchronization blocks in this class. in both Thread.sleep() is there but why here first main is getting finished and then only run completes. Really I am in Confusion
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Banu, try taking out the while(x==0){} statement in the run() method, and see what happens. If you don't see what's going on after a couple of minutes post another message and I will try to explain.
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya I got it . It is because of while loop. when x becomes 1 then only it comes out of while loop.


1. while runs
2. B prints and x=1
3. comes out of while loop and A prints....

Thank you Ruben...
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you got it Banu. You are actually implementing a "lock" in your code. But this is a very bad way to implement a lock, because the thread that is running the while loop is wasting valuable CPU cycles.
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the lock is implemented through while loop...
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain me what is happening in this program? From where do locks come into play and the while loop does not have a body of execution?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhi, when I said "lock" I didn't mean a real lock. What's going on is that the while() loop will continue executing until x is changed by main and its iteration condition becomes false.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, Ruben.
 
Don't touch me. And dont' touch this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic