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

  • Home
  • SEARCH
  • 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 9135479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:49:47+00:00 2026-06-17T08:49:47+00:00

I have 3 threads, that are currently running simultaneously. def f1(): print running first

  • 0

I have 3 threads, that are currently running simultaneously.

def f1():
    print "running first thread\n"
    sleep(10)

def f2():
    print "running second thread\n"
    sleep(10)

def f3():
    print "running third thread\n"
    sleep(10)


if __name__ == "__main__":
    thread1 = Thread(target = f1)
    thread2 = Thread(target = f2)
    thread3 = Thread(target = f3)

    try:
        thread1 = Thread(target = f1)
        thread1.start()

        thread2 = Thread(target = f2)
        thread2.start()

        thread3 = Thread(target = f3)   
        thread3.start()

        while(thread1.isAlive() or thread2.isAlive() or thread3.isAlive()): 
            thread1.join()
            thread2.join()
            thread3.join()  
    except (KeyboardInterrupt, SystemExit):
        sys.exit()

How can I simulate a deadlock? Also, how can I have each thread run after the other? Also can I list all the threads that are currently running in my script? or give them priorities ?

  • 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-17T08:49:48+00:00Added an answer on June 17, 2026 at 8:49 am

    How can I simulate a deadlock?

    All a deadlock means is that one or more threads are blocked from making any progress, so you can simulate it with a single thread. Just put a while True: around the sleep(10).

    In realistic cases, you usually have two threads blocking each other from progress at the same time. For example, maybe they’ve taken a pair of locks in reverse order, so thread 1 won’t release lock 1 until it gets lock 2, but thread 2 won’t release lock 2 until it gets lock 1. So, it might be better to simulate it by having two threads block permanently.

    If you want to actually create a deadlock, the simplest way is to have the threads literally block on each other: add thread2.join() to f1, and thread1.join() to f2. Then f1 can’t finish until f2 does, f2 can’t finish until f1 does, so neither one can ever finish.

    However, if you want to create a realistic deadlock, you will almost certainly want to use synchronization objects like threading.Lock to do something like the two-lock scenario described above.

    Also, how can I have each thread run after the other?

    Well, the easy way would be to not use threads in the first place. But if you really want, just do this:

    thread1.start()
    thread1.join()
    thread2.start()
    thread2.join()
    thread3.start()
    thread3.join()
    

    Also can I list all the threads that are currently running in my script?

    See threading.enumerate(). You usually don’t want to use this except for debugging purposes; keep track of the threads as you create them, if you want to have access to them later (as you’re doing).

    or give them priorities ?

    As the docs say:

    currently, there are no priorities, no thread groups, and threads cannot be destroyed, stopped, suspended, resumed, or interrupted.

    If you want this, you have to step outside of threading and, e.g., use native APIs via ctypes, win32api, etc.

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

Sidebar

Related Questions

I currently have a thread that I created using CreateRemoteThread(). Everything works great. Upon
I have multiple threads that share use of a semaphore. Thread A holds the
I have a custom thread pool class, that creates some threads that each wait
I know that there are some threads have a similar issue with this thread.
I have two threads, a producer thread that places objects into a generic List
I have a windows forms application that runs two threads simultaneously, with the UI
I currently have a Python 2.6 piece of code that runs two loops simultaneously.
I have an application that is currently running on IIS 6.0 with one worker
i have 20 threads that write with the println() function on a file called
I have few threads that performs an actvity in parallel. I would have to

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.