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

The Archive Base Latest Questions

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

I am desperate…I’m working all the day on a program but I didn’t resolve

  • 0

I am desperate…I’m working all the day on a program but I didn’t resolve my issue about thread cuncurrency.Please help me.

I have this class which is a generic Item.My problem is when this object enters in wait() it doesn’t wake up anymore even if I call on the same object the method putItemToWork().I would know if there is a mistake on the code about cuncurrency because really I don’t understand where I make mistakes…

Item Class

import java.io.*;

public class Item implements Serializable
{
private String id;
private String category;
private String machine;
private boolean isWorked;

private String mchTemp = null; 

public Item(String id,String category,String machine,boolean isWorked)
{
    this.id = id;
    this.category = category;
    this.machine = machine;
    this.isWorked = isWorked;
}

public synchronized void putItemToWork(String id_machine)
{    
    try
        {
            System.out.println("Working the item...");
            Thread.sleep((long)(1+Math.random()*10000));
        }
        catch(InterruptedException ie) {ie.printStackTrace(); }

            mchTemp = id_machine;        
    isWorked = true;
    notify();
}

public synchronized String getWorkedItem()
{
    if(mchTemp == null)
    {
        try
        {   
            wait();
        }
        catch(InterruptedException ie) {ie.printStackTrace(); }
    }

    return mchTemp;
}

public String getId()
{
    return this.id;
}

public String getCategory()
{
    return this.category;
}

public String getMachine()
{
    return this.machine;
}

public boolean isWorked()
{
    return this.isWorked;
}
}
}

ServerMultiThread

import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.SwingUtilities;
import javax.swing.JTextArea;

public class ServerMultiThread implements Runnable
{   
Socket socket;
private ServerSocket serverSocket;
private LinkedList<Item> itemsList;
private LinkedList<Machine> machinesList;
private static final boolean listening = true;
private JTextArea output;

public ServerMultiThread(LinkedList<Item> itemsList,LinkedList<Machine> machinesList,JTextArea output)
{
    this.itemsList = itemsList;
    this.machinesList = machinesList;

    this.output = output;

    try
    {
        this.serverSocket = new ServerSocket(8090);
    }
    catch(IOException ioe){ioe.printStackTrace(); }

    new Thread(this, "Server").start();
}

@Override
public void run()
{
    Item itemTemp = null;

    SwingUtilities.invokeLater(new Runnable(){@Override public void run(){output.append("Server in run!\n");}});

    while(listening)
    {

        try
        {
            SwingUtilities.invokeLater(new Runnable(){public void run(){output.append("Waiting for incoming connection...\n");}});

            socket = serverSocket.accept();

            SwingUtilities.invokeLater(new Runnable(){@Override public void run(){output.append("Connected to: "+socket.getInetAddress()+":"+socket.getPort()+"!\n");}});

            ObjectOutputStream ous = new ObjectOutputStream(socket.getOutputStream());

            synchronized(itemsList)
            {
                for(Item item : itemsList)
                {
                    if(!item.isWorked())
                    {
                        itemTemp = item;
                        break;
                    }
                }

                new ItemHandler(itemTemp,ous,output);
            }

        }
        catch(IOException ioe) {ioe.printStackTrace(); }
    }
}
}

ItemHandler

import java.io.*;
import java.util.LinkedList;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class ItemHandler implements Runnable
{
String id_machine;
private Item item;
private ObjectOutputStream ous;
//private ObjectInputStream ois;
private JTextArea output;

public ItemHandler(Item item,ObjectOutputStream ous,JTextArea output)
{
    this.item = item;
    this.ous = ous;
    //this.ois = ois;
    this.output = output;

    new Thread(this).start();
}

@Override
public void run()
{
    try
    {
        ous.writeObject(item);                    

        SwingUtilities.invokeLater(new Runnable(){public void run(){output.append("Item Handler "+item.getId()+ " in run!\n");}});

        id_machine = item.getWorkedItem();

        SwingUtilities.invokeLater(new Runnable(){public void run(){output.append("The item "+item.getId()+" was correctly worked by "+id_machine);}});

        //System.out.println("The item "+workedItem.getId()+" was correctly worked by "+workedItem.getMachine());
    }
    catch(IOException ioe){ioe.printStackTrace();}
}
}

MachineApp

import java.io.*;
import java.net.*;

public class MachineApp 
{
private Socket socket;
private String id_machine;

public MachineApp(String id_machine)
{
    this.id_machine = id_machine;

    try
    {
        this.socket = new Socket("localhost",8090);

        System.out.println("Estabilished connection to main server!");
    }
    catch(UnknownHostException uhe) {uhe.printStackTrace();}
    catch(IOException ioe) {ioe.printStackTrace(); }

    execution();
}

private void execution()
{
    try
    {
        //ObjectOutputStream ous = new ObjectOutputStream(socket.getOutputStream());    
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());          

        Item item = (Item) ois.readObject();

        item.putItemToWork(id_machine);

        System.out.println("Item Worked!");
    }
    catch(ClassNotFoundException cnfe) {cnfe.printStackTrace(); }
    catch(IOException ioe) {ioe.printStackTrace(); }
}

public static void main(String[] args)
{
    MachineApp machineApp = new MachineApp(args[0]);        
}
}
  • 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:16:55+00:00Added an answer on May 28, 2026 at 5:16 am

    For me your code works just fine. Are you calling wait/notify from different threads:

        public static void main(String[] args) {
        final Item item = new Item("id", "cat", "mach", false);
    
        Thread retrievalThread = new Thread(new Runnable() {
            @Override
            public void run() {
                item.getWorkedItem();
            }
        });
    
        Thread puttingThread = new Thread(new Runnable() {
            @Override
            public void run() {
                item.putItemToWork("id");
            }
        });
    
        retrievalThread.start();
        puttingThread.start();
    }
    

    EDIT: after client code was added to question:
    I could be wrong but you’re sending item object via socket and then trying to call getWorkedItem on it. It doesn’t work this way because once you’ve send item thru network object on the other side (even if it’s happening in one JVM) that will be different object. Therefore calling notify on it won’t trigger wake up from wait.

    How to solve it? Well, you can add some kind of query interface to your server code so you could query which items were worked.

    If it’s no homework, or learning exercise I think that Hadoop can be good fit for your needs

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

Sidebar

Related Questions

I am desperate with this issue, I set all the permissions correctly (I think),
First of all I am in DESPERATE need of help here PLEASE I will
I'm becoming quite desperate with this issue, and I've been trying all the ideas
I am quite desperate to resolve this very annoying issue :( I am trying
Desperate, please help. Will work for food :) I want to be able to
I am currently working on a PHP application that puts me in the desperate
First of all this is not homework, I'm in a desperate need for a
I'm quite desperate due to the following issue: How the hell :-) .. can
This might be really hacky, but I'm beginning to get desperate Is it possible
I'm desperate and frustrated about this : how can I show hide one or

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.