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 6912005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:02:00+00:00 2026-05-27T09:02:00+00:00

I have a thread doing things in background and I want to suspend it

  • 0

I have a thread doing things in background and I want to suspend it whenever my program has a real thing to do. Once that is finished I want the background thread to continue. E.g.

ta = Thread.new { loop { print 'a'; sleep 0.2 } }

sleep 0.5

# put ta to sleep
5.times { print 'b'; sleep 0.2 }
# let ta resume

sleep 0.8
puts

The output should be something like aaabbbbbaaaaa. Currently it is aaabababababaaaaa.

Although a thread can stop itself through Thread.stop, I found no method to stop a thread from “outside”. Is there a simple solution?

  • 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-05-27T09:02:00+00:00Added an answer on May 27, 2026 at 9:02 am

    What you need is a Mutex:

    $my_mutex = Mutex.new
    
    t1 = Thread.new do
      loop do
        $my_mutex.synchronize do
          print 'a'
        end
        sleep 0.2
      end
    end
    
    sleep 0.5
    
    $my_mutex.synchronize do
      5.times do
        print 'b'
        sleep 0.2
      end
    end
    
    sleep 0.8
    
    puts
    sleep
    

    Output:

    aaabbbbbaaaaa
    aaaaaaaaaaaaaaaaa...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UserControl with something that does some background work. When thing has
In an android service I have created thread(s) for doing some background task. I
I have a background thread (NSOperation) that I use to get new messages for
Given client = new XMemcachedClient(server, port); What happens if I have several thread doing
I have thread where downloading xml file a this xml file i want to
I have a thread which needs to be run in the background every 1000ms.
I have a thread from a thread pool in a service that does quite
I have several thread pools and I want my application to handle a cancel
I have a thread in Ruby. It runs a loop. When that loop reaches
I have a GUI application that needs to do something simple in the background

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.