Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

could you find a methaphor about what happen when two threads are synchronized  RSS feed

 
Giovanni Montano
Ranch Hand
Posts: 326
4
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
extend this metaphor when there is a synchronized statement( ex inside a method synchronize(this))

I challenge you!
it is not immediately graspable from my side, thank you although I am going through the java docs and some tutorials, this concept remains to me still quite theorical, I cannot form an image, can you do it with a metaphor? thank you
 
Giovanni Montano
Ranch Hand
Posts: 326
4
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:extend this metaphor when there is a synchronized statement( ex inside a method synchronize(this))

I challenge you!
it is not immediately graspable from my side, thank you although I am going through the java docs and some tutorials, this concept remains to me still quite theorical, I cannot form an image, can you do it with a metaphor? thank you


I auto reply to my self.
first of all the thread are for me like the prisoner dilemma I studied in the Economics classes( of school, not of Java).
The prisoner dilemma is over two prisoners that commit a crime. If they are interrogated into two different rooms( each room is a thread) they do not know what to do.
should I confess, should i not confess, what if the other fellow has confessed or not confessed, here is the dilemma, the lock ( the thread executor is changing to another stack or not). Synchronize them, means to convince each prisoner in the room( thread) than the other has confessed the crime, before 1 and then the 2 one so that with two confessions the judge sir Jan van Marteen( JVM), great sublime master emperor of the judging word can "execute" really fast the punishment.

a synchronized statement is the same, but in this case instead of the interrogate all the prisoners, they inject a secret service syring made by the secret company Synchronized(this.mouth) LTD into the neck of the two prisoners so that they isolate the mouth of both of them so that they have to say the truth! in this way you obtain the same result( method synchronization or synchronization statement
 
Campbell Ritchie
Sheriff
Posts: 53740
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when the prisoners have finished the interrogation, by the nice cops and the nasty cops, and the nice cops have poured large quantities of the elixir of life into them (we are into CS, so we know that means Coffee), the prisoners will have to go for a pee. And when they go out there, they can meet each other and tell each other what has happened. The synchronisation works like this:-
  • 1: One prisoner goes for pee but must lock the door. Other prisoner cannot gain access.
  • 2: Prisoner finishes, pulls chain, washes hands, and comes out.
  • 3: Only now can other prisoner go in.
  • Since the prisoners are into CS too, there is no point in writing on the wall: they know nobody can read their writing

  • 1: Thread 1 enters method. Object on which synchronised locks its lock. No other threads can gain access to methods synchronised on that lock.
  • 2: Thread 1 completes method and exits. Lock is now unlocked and other threads can gain access.
  •  
    Giovanni Montano
    Ranch Hand
    Posts: 326
    4
    Android Open BSD Slackware
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:But when the prisoners have finished the interrogation, by the nice cops and the nasty cops, and the nice cops have poured large quantities of the elixir of life into them (we are into CS, so we know that means Coffee), the prisoners will have to go for a pee. And when they go out there, they can meet each other and tell each other what has happened. The synchronisation works like this:-
  • 1: One prisoner goes for pee but must lock the door. Other prisoner cannot gain access.
  • 2: Prisoner finishes, pulls chain, washes hands, and comes out.
  • 3: Only now can other prisoner go in.
  • Since the prisoners are into CS too, there is no point in writing on the wall: they know nobody can read their writing

  • 1: Thread 1 enters method. Object on which synchronised locks its lock. No other threads can gain access to methods synchronised on that lock.
  • 2: Thread 1 completes method and exits. Lock is now unlocked and other threads can gain access.

  • thank you Sherif to follow me in this crazy story, but it was tremendously useful to grasp the surface of this concurrency world.

     
    Campbell Ritchie
    Sheriff
    Posts: 53740
    127
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!