Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lock Mechanism Doubt

 
Lee Mark
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



please explain the behaviour of this code..

 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm not sure what do you want from us to explain, so I've added a few comments.
Lee Mark wrote:please explain the behaviour of this code.


Hope this helps.

Regards,
Rok
 
Lee Mark
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but what if i create i new for every thread befor thay start i am getting same output i tried it running atleast 15 itimes to ensure tht..


i am adding

mr.i = new Integer(2);

before each thread start call..
comment again
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Lee Mark wrote:but what if i create i new for every thread befor thay start i am getting same output i tried it running atleast 15 itimes to ensure tht..


i am adding

mr.i = new Integer(2);

before each thread start call..
comment again

If I understood you correctly, you've suggested following:
You have to understand that threads are not really executed in parallel, however in a sequence (handled by OS).
When you call start() on a thread, this just means that the thread is ready to run, however whether it will be started or not is up to the scheduler.
So, the scheduler determines which thread will be started at what time...
Therefore the result could/would be the same as without above changes, because all running threads will have to gain the lock on e.g. Integer(3).

Hope I answered your question.


Regards,
Rok
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic