Python多线程面试题求解及t1.start()类型错误排查
Hey there! Let's tackle your problem from two angles first: fixing that frustrating TypeError, then getting your multi-threaded sequence output working correctly.
First: Fixing the 'int' object is not callable Error
That error almost always happens when you accidentally try to call the start() method on an integer variable instead of a thread object. Here are the two most common causes:
- You named your thread instance something like
t1, but earlier in your code you used the same variable name to store an integer (e.g.,t1 = 5). When you later tryt1.start(), Python thinks you're trying to call an integer like a function. - In your custom thread class, you accidentally overwrote the required
run()method with an integer value (e.g.,self.run = 0). Threads rely on therun()method to execute their logic, so this breaks everything.
Quick fix: Double-check your variable names to avoid conflicts, and make sure your thread classes properly define a run() method (not an integer attribute).
Second: Implementing the Sequential Multi-Threaded Output
To get the sequence 010203040506, we need strict synchronization between the three threads. We'll use threading.Condition (a condition variable) to control which thread runs at each step. Here's a complete, working implementation:
import threading # Global synchronization tools and state condition = threading.Condition() current_turn = 0 # 0=zero thread, 1=odd thread, 2=even thread max_odd = 5 max_even = 6 odd_counter = 1 even_counter = 2 class ZeroThread(threading.Thread): def run(self): global current_turn, odd_counter, even_counter # We need 6 zeros (one for each number 1-6) for _ in range(6): with condition: # Wait until it's this thread's turn while current_turn != 0: condition.wait() print("0", end="") # Switch turn to odd or even based on next number needed if odd_counter <= max_odd and (odd_counter - 1) == even_counter // 2: current_turn = 1 else: current_turn = 2 condition.notify_all() class OddThread(threading.Thread): def run(self): global current_turn, odd_counter while odd_counter <= max_odd: with condition: while current_turn != 1: condition.wait() print(odd_counter, end="") odd_counter += 1 # Switch back to zero thread after output current_turn = 0 condition.notify_all() class EvenThread(threading.Thread): def run(self): global current_turn, even_counter while even_counter <= max_even: with condition: while current_turn != 2: condition.wait() print(even_counter, end="") even_counter += 1 # Switch back to zero thread after output current_turn = 0 condition.notify_all() if __name__ == "__main__": # Create thread instances (no name conflicts here!) zero_thread = ZeroThread() odd_thread = OddThread() even_thread = EvenThread() # Start the threads zero_thread.start() odd_thread.start() even_thread.start() # Wait for all threads to finish zero_thread.join() odd_thread.join() even_thread.join() print() # Clean up the output with a newline
How This Works:
- Condition Variable: The
conditionobject lets threads wait until their turn comes, and notify others when it's time to switch. - Turn Control:
current_turnacts as a traffic light—only the thread matching the current turn can execute. - Thread Logic:
- The
ZeroThreadruns 6 times (once for each number in 1-6), prints0, then switches the turn to either the odd or even thread based on which number is needed next. - The
OddThreadprints 1,3,5 in order, then switches back to the zero thread. - The
EvenThreadprints 2,4,6 in order, then switches back to the zero thread.
- The
Run this code, and you'll get the exact sequence 010203040506 you need!
内容的提问来源于stack exchange,提问作者Melissa Stewart

