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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:06:21+00:00 2026-06-09T08:06:21+00:00

I have one query that is I have developed a code below of multiple

  • 0

I have one query that is I have developed a code below of multiple threads named thread one and thread two, below is the code ..

class multip implements Runnable {

public void run() {
    for (int i = 0; i < 20; i++) {
        try {


        Thread.sleep(500);
                System.out.println(Thread.currentThread().getName());
                System.out.println("i");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

public class MultiThread3 {
    public static void main(String... a) {
        multip obj = new multip();
        Thread t1 = new Thread(obj);
        t1.currentThread().setName("one");
        t1.start();
        Thread t2 = new Thread(obj);
        t2.currentThread().setName("two");
        t2.start();

    }

}

Now I want thread two be set as daemon thread and it should provide some service to thread one it could be any kind of service but at last What I was trying to achieve is that a daemon thread providing some service to non daemon thread. Please advise how to achieve this.The question is about of how the daemon thread will provide the service to non daemon thread..?

  • 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-09T08:06:24+00:00Added an answer on June 9, 2026 at 8:06 am

    The question is about of how the daemon thread will provide the service to non daemon thread

    I would use an executor service. If you want to return a value from the daemon thread, you can use a Callable<ReturnType> instead of a Runnable.

    // creating a thread pool.
    ExecutorService service = Executors.newSingleThreadExecutor(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            // creating a thread.
            Thread two = new Thread(r, "two");
            // making it a daemon thread.
            two.setDaemon(true);
            return two;
        }
    });
    
    for(int i=0;i<10;i++)
        // creating a task and submitting it.
        service.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("["+Thread.currentThread().getName()+"] - Hello World.");
                Thread.yield();
            }
        });
    service.shutdown();
    

    prints

    [two] - Hello World.
    [two] - Hello World.
    [two] - Hello World.
    

    First it creates a thread pool with a work queue. The thread pool has a factor which creates threads, in this case with a given name which is a daemon.

    Secondly there is a loop which add 10 tasks to the queue for the executors thread(s) to execute.

    Finally it stops the service when it has finished with it (this is rarely needed)

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

Sidebar

Related Questions

I have a query that returns a result set similar to the one below:
I have a mysql query that uses union to join multiple queries into one
I have a query that updates one record, and only one record. Is there
I have a query that always returns one row, with many columns. I would
I have a query that returns one row. However, I want to invert the
I have a query that I know it should atleast return one row. How
I have a sub query that returns one column, showing as GroupType, I then
I have a query that shows me a listing of ALL opportunities in one
Lets say that we have a query like this one: SELECT *, (CUSTOM_EXPRESSION) as
I have one array that is filled by mysql_query. Values that are in this

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.