You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python多线程面试题求解及t1.start()类型错误排查

解决多线程输出序列问题 & 修复TypeError

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 try t1.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 the run() 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:

  1. Condition Variable: The condition object lets threads wait until their turn comes, and notify others when it's time to switch.
  2. Turn Control: current_turn acts as a traffic light—only the thread matching the current turn can execute.
  3. Thread Logic:
    • The ZeroThread runs 6 times (once for each number in 1-6), prints 0, then switches the turn to either the odd or even thread based on which number is needed next.
    • The OddThread prints 1,3,5 in order, then switches back to the zero thread.
    • The EvenThread prints 2,4,6 in order, then switches back to the zero thread.

Run this code, and you'll get the exact sequence 010203040506 you need!

内容的提问来源于stack exchange,提问作者Melissa Stewart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:49:07