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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:35:02+00:00 2026-06-14T19:35:02+00:00

So I have been trying to write a java program, that involves Threads. It

  • 0

So I have been trying to write a java program, that involves Threads. It is basically a problem where a thread asks a shared memory resource(cell class in the code below) for permission to move. I’ll show you an example of the code in the thread. The three mentioned methods from the cell class are synchronized methods, however they don’t implement wait or notify’s.

public void run() {
    try{
        while(true){
            Random r = new Random();
            Thread.sleep(r.nextInt(1000));
            //asks the class cell permission to move if the cell is free.
            if(cell.asksAccess(xi, yi, xf, yf)){
                cell.releaseCell(xi, yi); //release the previous cell
                move(); // move the object
                cell.blockCell(xi, yi); // blocks the cell where the object is now staying.
                setChanged();
                notifyObservers();
            }
        }
    } catch (InterruptedException e) {
    }
}

private void move() {
    int dx = xf - xi;
    int dy = yf - yi;

    xi += (int) Math.signum(dx);
    yi += (int) Math.signum(dy);
}

Like I said before, all the cell class methods invoked are synchronized. My problem is that this is not working as I expected and when I did a sysout to debug, it showed that the threads are not always moving forward and sometimes they even move back to where they were in the beginning and I can’t understand why since the move method always tells them to go forward and never back. Is this a sync problem with the cell class? Or is it the move method? Any help would be a lot appreciated.

  • 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-14T19:35:03+00:00Added an answer on June 14, 2026 at 7:35 pm

    First, lets consider the move method. Assuming that:

    • the xi, xf, yi and yf variables are instance variables of the Runnable, and
    • there are no other (callback) methods that access or update them,

    then we can conclude that those variables are “thread confined” and no synchronization is required.

    So where is the problem? Well I think it is here:

    if (cell.asksAccess(xi, yi, xf, yf)) {
        cell.releaseCell(xi, yi); //release the previous cell
        move(); // move the object
        cell.blockCell(xi, yi); // blocks the cell where the object is now staying.
        setChanged();
        notifyObservers();
    }
    

    I can see two issues:

    • I think you said that askCell, blockCell and releaseCell synchronize on the cell object. But the problem is that the sequence “ask, block, release” needs to be atomic.
    • Your code seems to be releasing a cell(?) at xi, yi … and then blocking it again. The comments seem to imply that you should be calling cell.blockCell(xf, yf) instead.

    … what is the best way to take care of the synchronization of that sequence?

    Without seeing the related code, it is hard to say what the best way is. But one way to do it would be to make it one call on the cell object … and deal with the move and notifications afterwards; e.g. something like this:

    if (cell.checkAndMove(xi, yi, xf, yf)) {
        move(); // move the object
        setChanged();
        notifyObservers();
    }  
    

    where checkAndMove is defined in the Cell class as:

    public synchronized boolean checkAndMove(int xi, int yi, int xf, int yf) {
        boolean ok = this.asksAccess(xi, yi, xf, yf);
        if (ok) {
            this.releaseCell(xi, yi);
            this.blockCell(xi, yi);
        }
        return ok;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been trying to write a program in python to read two text
I have been trying to write a regex that will remove whitespace following a
I am trying to write a java program that will automatically download and name
I program in Java and have been trying to understand exactly what operator overloading
Hey guys i have been trying to make a NIO server/client program. My problem
i have been trying to write some programs on java sockets and i got
I have a database and a Java Program. I am trying to write a
I have started learning Java and am having problem with a simple program that
I have been trying to write some Java bytecode and assemble it using Jasmin.
In trying to write more testable Java code, I have been using the Model-View-Presenter

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.