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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:16:48+00:00 2026-05-21T10:16:48+00:00

Good day! I’ve meet problems with synchronizing threads in Java. I have 3 processes

  • 0

Good day!

I’ve meet problems with synchronizing threads in Java.

I have 3 processes ( i keep them in ConcurrentHashMap< Integer, Integer > man ) and 4 resources ( i keep them in ConcurrentHashMap< Integer, Integer > resourses ).

Each process can hold only 2 immediate resources. For example man[0] can hold only resources[0] and resources[1], man[1] can hold only resources[1] and resources[2] and etc.

So i have few situations when thread must wait till other threads will not release their resources. So this thread goes to wait… And it doesn’t wake when i call notifyAll()!

What is wrong with my logic?

Here is my code:

import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Philos
{
    private Thread thisThread;

public Philos()
{
    for ( int i = 1; i <= 4; i++ )                                          //Init processeses and resourses.
    {
        resourses.put( i, 0 );                                              //Each is free.
        man.put(i, 0);
    }
}


public void startThread( final int i )
{
    thisThread = new Thread()
    {
         public void run()
         {

             try {
                System.out.println(Thread.currentThread().getName());
                Eat( i );                                                   //Proc try to start work
                show( i, "Eat" );                                           //Show res and procs

                finishEat( i );                                             //Release resourses.
                show( i, "Release" );
                thisThread.interrupt();                                     //Finish threads
            } catch (InterruptedException ex) {
                Logger.getLogger(Philos.class.getName()).log(Level.SEVERE, null, ex);
            }
         }


    };

    thisThread.start();

}


private synchronized  void Eat( int i ) throws InterruptedException
    {
        testEat ( i );                                                          //Try to work.
        if ( !( man.get(i) == 1 ) )                                             //If proc are not working
        {
            System.out.printf("%d must wait\n", i);
            synchronized( Thread.currentThread() )                              //Put synchronize block to wait thread
            {
               Thread.currentThread().wait();                                   //Thread must wait
               System.out.println("Notifyied");
            }
        }

    }

private synchronized  void finishEat( int i ) throws InterruptedException
{
            man.put( i, 0 );                                                        //Release proc
            resourses.put(i,   0);                                                  //Release resourses of proc
            resourses.put(i+1, 0);
}


private synchronized  void testEat( int i ) throws InterruptedException
{
        System.out.printf("Test %d\n",i);
        if ( ( left(i) == 0 ) && ( right(i) == 0 ) )                            //If resourses availible
        {
            man.put( i, 1 );                                                    //Proc is busy
            resourses.put(i,   1);                                              //Resourses are busy
            resourses.put(i+1, 1);

            synchronized( Thread.currentThread() )
            {
                Thread.currentThread().notifyAll();                             //Notify al threads
            }
        }
}


private int left( int i )                                                   //Check left resourse.
    {
        return (resourses.get(i));
    }

    private int right( int i )                                                  //Check right resourse.
    {
        return (resourses.get(i+1));
    }

    private synchronized  void show( int i, String info )                       //Show info about resourses and proc
    {
        System.out.printf("\nid = %d, [%s]\n", i, info );

        System.out.println("Work[R]:");
        for ( int j = 1; j <= 4; j++ )                                          //Res
        {
            System.out.printf( "%d ", resourses.get(j) );
        }
        System.out.println();
        for ( int j = 1; j <= 4; j++ )                                          //Proc
        {
            System.out.printf( " %d", man.get(j) );
        }
        System.out.println();
    }


    private ConcurrentHashMap< Integer, Integer > resourses = new ConcurrentHashMap< Integer, Integer >();
    private ConcurrentHashMap< Integer, Integer > man = new ConcurrentHashMap< Integer, Integer >();
}
  • 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-21T10:16:49+00:00Added an answer on May 21, 2026 at 10:16 am

    You’re calling wait() and notifyAll() on a Thread object. That’s not the way of doing things – you call it on some common object which all the relevant threads hold.

    The fact that you’re only synchronizing on the current thread in each case is also meaningless – nothing else is going to synchronize on that thread, so the synchronization is pointless.

    You need to determine the common objects to synchronize/wait/notify. Typically I prefer to create separate objects solely for this purpose, but you can reuse existing objects too.

    (I would also advise you not to call Thread.interrupt – it’s unclear to me why you’re doing that here…)

    EDIT: As mentioned in comments, look at the higher-level abstractions in java.util.concurrent, such as Semaphore, for a possibly-simpler way of achieving your goal.

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

Sidebar

Related Questions

Good day, I have a parent gridview with a few columns. On each row,
Good day, In SQL Server 2005, I have a table numerous columns, including a
Good Day! i have installed xcode latest in my snow leopard, now i have
Good day. I have to pass the session value to a business logic layer,
Good day, I have a question here. I am trying to fetch image from
Good day, I have this problem with Html.DropDownListFor which I can't seem to work
Good day shell lovers! basically i have two files: frequency.txt: (multiple lines, space separated
Good day! I encountered the following error upon running my JSP program. java.lang.IllegalStateException: PWC3991:
Good day! I tried using JSTL in java but there's an error: exception javax.servlet.ServletException:
Good day, We just converted our web application .NET 1.1 to .NET 2.0. We

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.