Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8935673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:02:40+00:00 2026-06-15T10:02:40+00:00

import threading x = 0; class Thread1(threading.Thread): def run(self): global x for i in

  • 0
import threading

x = 0;

class Thread1(threading.Thread):
    def run(self):
        global x
        for i in range(1,100000):
            x = x + 1

class Thread2(threading.Thread):
    def run(self):
        global x
        for i in range(1,100000):
            x = x - 1

#create two threads
t1 = Thread1()
t2 = Thread2()

#start the threads
t1.start()
t2.start()

#wait for the threads to finish
t1.join()
t2.join()

print x;

Running this multiple times produces different output, some in the negative and some in the positive. Is it because the two threads are using the same global x? I don’t understand why: shouldn’t the net effect (output) be the same after all the dust settles?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:02:43+00:00Added an answer on June 15, 2026 at 10:02 am

    Not necessarily. Imagine the following sequence of events. We’ll begin at a precise moment after the program has been running for a bit; both Thread1 and Thread2 are inside their for loops, and x = 0

    1. Thread1 has control. It accesses x to determine the value of x + 1; x is 0, so the result is 1. However…
    2. Before Thread1 completes the assignment, control passes to Thread2. x is still 0.
    3. Thread2 now accesses x. It calculates x - 1, which is -1, because x is still 0. Because of the unpredictability of thread timing, it manages to complete the operation, assigning -1 to x.
    4. Control now returns to Thread1. It has already calculated the value of x + 1 to be 1. It assigns 1 to x.

    Both threads have completed an iteration, and the value of x should be 0, but its actual value is 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

from threading import Thread import time print 'start of script' class MyThread(Thread): def run(self):
import subprocess import threading import StringIO class terminal(threading.Thread): def run(self): self.prompt() def prompt(self): x
import time import threading class test(threading.Thread): def __init__ (self): threading.Thread.__init__(self) self.doSkip = False self.count
I have this class: from threading import Thread import time class Timer(Thread): def __init__(self,
import threading import time class Eat(threading.Thread): def __init__(self, surname): self.counter = 0 self.surname =
Say I derive from threading.Thread: from threading import Thread class Worker(Thread): def start(self): self.running
Im trying to execute a program in a python subprocess: class MiThread(threading.Thread): def __init__(self):
from google.appengine.ext import db class Log(db.Model): content = db.StringProperty(multiline=True) class MyThread(threading.Thread): def run(self,request): #logs_query
I am testing Python threading with the following script: import threading class FirstThread (threading.Thread):
I am using threading.py and I have the following code: import threading class MyClass(threading.Thread):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.