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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:47:18+00:00 2026-06-01T22:47:18+00:00

What I’m trying to do is have a thread write a message it received

  • 0

What I’m trying to do is have a thread write a message it received from its parent thread to an OutputStream, listen to an InputStream for the reply and then notify the parent thread with the reply. I wrote two test classes that do something similar but simpler in different ways.
Method 1 only works when the "before loop" debug statement is uncommented, method 2 only prints the "message from child" debug statements. What am I doing wrong?

Method 1

public class Parent {
    private static int out = 0;
    private static int in = 0;

    public static void main(String[] args) {
        final Object locker = new Object();
        Thread t = new Thread(new Runnable() {          
            @Override
            public void run() {
                while (true) {
                    synchronized (locker) {
                        try {
                            locker.wait();
                            System.out.println("Message from parent " + out);
                            in = out + 10;
                            locker.notify();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        t.start();

        System.out.println("before loop");
        while (out < 10) {
            synchronized (locker) {
                locker.notify();
                try {
                    locker.wait();
                    out++;
                    System.out.println("Message from child " + in);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }               
            }   
        }       
    }
}

Method 2

public class Parent {

    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
        final BlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(1);

        Thread t = new Thread(new Runnable() {          
            @Override
            public void run() {
                while (true) {
                    try {
                        Integer i = q.take();               
                        System.out.println("Message from parent: " + i.intValue());
                        q.put(i.intValue() + 10);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }               
            }
        });

        for (int i = 0; i < 10; i++) {
            q.put(i);
            Integer j =  q.take();
            System.out.println("Message from child: " + j);
        }
    }

}
  • 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-01T22:47:19+00:00Added an answer on June 1, 2026 at 10:47 pm

    The Java API already offers that functionality; do you have a valid reason to reinvent the wheel?

    public static void main(String[] args) throws Exception {
        // start a worker thread
        ExecutorService exec = Executors.newFixedThreadPool(1);
    
        // ask the worker thread to execute a task (
        Future<String> future = exec.submit(() -> {
            Thread.sleep(500); // simulate waiting for I/O
            return "hello from child";
        });
    
        // get the return value from the worker thread (waiting until it is available)
        String greeting = future.get();
        System.out.println(greeting);
    
        // terminate the worker thread (otherwise, the thread will wait for more work)
        exec.shutdown();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
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
For some reason, after submitting a string like this Jack’s Spindle from a text

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.