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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:59:35+00:00 2026-05-21T06:59:35+00:00

In most cases when you create your thread you can prepare the data beforehand

  • 0

In most cases when you create your thread you can prepare the data beforehand and pass it into the constructor or method.

However in cases like an open socket connection you will typically already have a thread created but wish to tell it to perform some action.

Basic idea:

C#

private Thread _MyThread = new Thread(MyMethod);
this._MyThread.Start(param);   

Java

private Thread _MyThread = new Thread(new MyRunnableClass(param));
this._MyThread.start();

Now what?

So what is the correct way to pass data to a running thread in C# and Java?

  • 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-21T06:59:36+00:00Added an answer on May 21, 2026 at 6:59 am

    One way to pass data to a running thread is by implementing Message Queues. The thread that wants to tell the listening thread to do something would add an item to the queue of the listening thread. The listening thread reads from this thread in a blocking fashion. Causing it to wait when there are no actions to perform. Whenever another thread puts a message in the queue it will fetch the message, depending on the item and it’s content you can then do something with it.

    This is some Java / pseudo code:

    class Listener
    {
       private Queue queue;
       public SendMessage(Message m)
       {
         // This will be executed in the calling thread.
         // The locking will be done either in this function or in the Add below
         // depending on your Queue implementation.
         synchronize(this.queue) 
         {
            this.queue.put(m);
         }
       }
    
       public Loop()
       {
         // This function should be called from the Listener thread.
         while(true) 
         {
            Message m = this.queue.take();
            doAction(m);
         }
       }
    
       public doAction(Message m)
       {
          if (m is StopMessage)
          {
            ...
          }
       }
    }
    

    And the caller:

    class Caller
    {
      private Listener listener;
    
      LetItStop()
      {
         listener.SendMessage(new StopMessage());
      }
    }
    

    Of course, there are a lot of best practices when programming paralllel/concurrent code. For example, instead of while(true) you should at the least add a field like run :: Bool that you can set to false when you receive a StopMessage. Depending on the language in which you want to implement this you will have other primitives and behaviour to deal with.

    In Java for example you might want to use the java.util.Concurrent package to keep things simple for you.

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

Sidebar

Related Questions

What's the most important consideration when deciding how to partition your application into assemblies?
In most cases I am used to work with Windows, some weeks ago I
I have a large Compact Frameworks V2.0 application that in most cases works very
Dealing with Java - which the option you prefer in the most cases -
I refactor some classes from standard SQL to JPA/ORM usage. In most cases the
I agree, that programming against interfaces is a good practice. In most cases in
When are custom Exception classes most-valuable? Are there cases when they should or should
Like most people new to Git, I've had my share of confusion trying to
Hi i do a lot of research on the web but at most cases
Let's say I have two Databases like so : DatabaseA create table Table1 (

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.