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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:46:43+00:00 2026-05-28T05:46:43+00:00

I’m loosely following a tutorial on Java NIO to create my first multi-threading, networking

  • 0

I’m loosely following a tutorial on Java NIO to create my first multi-threading, networking Java application. The tutorial is basically about creating an echo-server and a client, but at the moment I’m just trying to get as far as a server receiving messages from the clients and logging them to the console. By searching the tutorial page for "EchoServer" you can see the class that I base most of the relevant code on.

My problem is (at least I think it is) that I can’t find a way to initialize the queue of messages to be processed so that it can be used as I want to.

The application is running on two threads: a server thread, which listens for connections and socket data, and a worker thread which processes data received by the server thread. When the server thread has received a message, it calls processData(byte[] data) on the worker, where the data is added to a queue:

1.  public void processData(byte[] data) {
2.      synchronized(queue) {
3.          queue.add(new String(data));
4.          queue.notify();
5.      }
6.  }

In the worker thread’s run() method, I have the following code:

7.  while (true) {
8.      String msg;
9.
10.     synchronized (queue) {
11.         while (queue.isEmpty()) {
12.             try {
13.                  queue.wait();
14.             } catch (InterruptedException e) { }
15.         }
16.         msg = queue.poll();
17.     }
18.
19.     System.out.println("Processed message: " + msg);
20. }

I have verified in the debugger that the worker thread gets to line 13, but doesn’t proceed to line 16, when the server starts. I take that as a sign of a successful wait. I have also verified that the server thread gets to line 4, and calls notify()on the queue. However, the worker thread doesn’t seem to wake up.

In the javadoc for wait(), it is stated that

The current thread must own this object’s monitor.

Given my inexperience with threads I am not exactly certain what that means, but I have tried instantiating the queue from the worker thread with no success.

Why does my thread not wake up? How do I wake it up correctly?


Update:

As @Fly suggested, I added some log calls to print out System.identityHashCode(queue) and sure enough the queues were different instances.

This is the entire Worker class:

public class Worker implements Runnable {
    Queue<String> queue = new LinkedList<String>();
    public void processData(byte[] data) { ... }
    @Override
    public void run() { ... }
}

The worker is instantiated in the main method and passed to the server as follows:

public static void main(String[] args)
{
    Worker w = new Worker();
    // Give names to threads for debugging purposes
    new Thread(w,"WorkerThread").start();
    new Thread(new Server(w), "ServerThread").start();
}

The server saves the Worker instance to a private field and calls processData() on that field. Why do I not get the same queue?


Update 2:

The entire code for the server and worker threads is now available here.

I’ve placed the code from both files in the same paste, so if you want to compile and run the code yourself, you’ll have to split them up again. Also, there’s abunch of calls to Log.d(), Log.i(), Log.w() and Log.e() – those are just simple logging routines that construct a log message with some extra information (timestamp and such) and outputs to System.out and System.err.

  • 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-28T05:46:44+00:00Added an answer on May 28, 2026 at 5:46 am

    I’m going to guess that you are getting two different queue objects, because you are creating a whole new Worker instances. You didn’t post the code that starts the Worker, but assuming that it also instantiates and starts the Server, then the problem is on the line where you assign this.worker = new Worker(); instead of assigning it to the Worker parameter.

        public Server(Worker worker) {
                this.clients = new ArrayList<ClientHandle>();
                this.worker = new Worker(); // <------THIS SHOULD BE this.worker = worker;
                try {
                        this.start();
                } catch (IOException e) {
                        Log.e("An error occurred when trying to start the server.", e,
                                        this.getClass());
                }
        }
    

    The thread for the Worker is probably using the worker instance passed to the Server constructor, so the Server needs to assign its own worker reference to that same Worker object.

    • 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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am reading a book about Javascript and jQuery and using one of the

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.