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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:21:25+00:00 2026-05-30T12:21:25+00:00

I’m trying to create a simple multithreaded program with jRuby. It needs to start

  • 0

I’m trying to create a simple multithreaded program with jRuby. It needs to start and stop threads based on a specified amount of time e.g. run for five seconds then stop. I’m pretty new to this sort of stuff, so it’s probably pretty basic but I can’t get it to work.

The relevant code looks like this:

require 'java'
require 'timeout'
require './lib/t1.rb'
require './lib/t2.rb'

class Threads
  [...]

  def manage_threads
    thread2 = T2.new
    # Wait for 5 seconds before the thread starts running..
    thread2.run(wait_time = 5)

    Timeout::timeout(10) do
      thread1 = T1.new {}
    end
  end

class T1 < Thread  

  def initialize
    while super.status != "sleep"
      puts "Thread 1"      
      sleep(1)
    end
  end
end

class T2
  include java.lang.Runnable

  def run wait_time
    thread = Thread.new do
      sleep(wait_time)
      loop do
        puts "Thread 2"
        sleep(1)
      end
    end
  end

  def stop_thread(after_run_time)
    sleep(after_run_time)
  end
end

I have already tried a couple if things, for example:

# Used timeout 
Timeout::timeout(10) do
   thread1 = T1.new {}
end

# This kinda works, except that it terminates the program and therefore isn't the behavior 
# I want. 

Does anyone have a suggestion on how to 1. start a thread, run it for a while. 2. Start a new thread, run both thread in parallel. 2. Stop thread 1 but keep running thread 2. Any tips/suggestions would be appreciated.

  • 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-30T12:21:27+00:00Added an answer on May 30, 2026 at 12:21 pm

    I think I solved it.

    This did the trick:

    def run wait_time
      thread = Thread.new do
        sleep(wait_time)
        second_counter = 0
        loop do
          puts "Thread 2"
          second_counter += 1
          if second_counter == 15
            sleep
          end
          sleep(1)
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Specifically, suppose I start with the string string =hello \'i am \' me And
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.