title: Python: execution order in Threading
body: <p>This is a code for testing <code>Threading</code>:</p>
  
  <pre><code>import threading
  import time
  
  
  episode = 0
  lock = threading.Lock()
  
  class Agent(threading.Thread):
      def __init__(self, id):
          threading.Thread.__init__(self)
          self.id = id
  
      def run(self):
          global episode
          while episode &lt; 5:
              with lock:
                  print(
                      "{} : episode will be changed {} -&gt; {}".format(
                          self.id,
                          episode,
                          episode+1
                      )
                  )
                  episode += 1
                  print("{} : changed value -&gt; {}".format(self.id, episode))
                  time.sleep(1)
  
  
  if __name__ == "__main__":
      agents = []
      for i in range(3):
          agents.append(Agent(i))
  
      for agent in agents:
          agent.start()
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>0 : episode will be changed 0 -&gt; 1
  0 : changed value -&gt; 1
  0 : episode will be changed 1 -&gt; 2
  0 : changed value -&gt; 2
  0 : episode will be changed 2 -&gt; 3
  0 : changed value -&gt; 3
  0 : episode will be changed 3 -&gt; 4
  0 : changed value -&gt; 4
  0 : episode will be changed 4 -&gt; 5
  0 : changed value -&gt; 5
  1 : episode will be changed 5 -&gt; 6
  1 : changed value -&gt; 6
  2 : episode will be changed 6 -&gt; 7
  2 : changed value -&gt; 7
  </code></pre>
  
  <p>This is one of result that I had expected:</p>
  
  <pre><code>0 : episode will be changed 0 -&gt; 1
  0 : changed value -&gt; 1
  2 : episode will be changed 1 -&gt; 2
  2 : changed value -&gt; 2
  1 : episode will be changed 2 -&gt; 3
  1 : changed value -&gt; 3
  2 : episode will be changed 3 -&gt; 4
  2 : changed value -&gt; 4
  0 : episode will be changed 4 -&gt; 5
  0 : changed value -&gt; 5
    .
    .
  </code></pre>
  
  <p>I can not understand why thread id=0 keep appearing consecutively in the first place... As I know of, the execution order of threads is random, right?</p>
  
  <p>What's wrong in my code? </p>