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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:19:02+00:00 2026-05-15T12:19:02+00:00

The code below is to emulate a robotics simulator I’m working with. I’m not

  • 0

The code below is to emulate a robotics simulator I’m working with. I’m not entirely sure why this doesn’t work – I’m not very familiar with threads and even though I’ve tried reading plenty today, I don’t seem to be making progress. The problem is that once pauseDistanceSensor() is called, it never wakes up.

import java.util.Random;

public class TestThreads
{
    private DistanceSensor dist;
    private Thread distanceThread;
    public TestThreads()
    {
        this.dist = new DistanceSensor();
        this.distanceThread = new Thread(this.dist);
        this.distanceThread.start();
    }

    public int getDistance()
    {
        return this.dist.getMeasurement();
    }

    public void pauseDistanceSensor()
    {
        synchronized(this.dist)
        {
            try {
                this.dist.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void resumeDistanceSensor()
    {
        synchronized(this.dist)
        {
            this.dist.notify();
        }
    }

    public static void main(String[] args)
    {
        TestThreads test = new TestThreads();
        long timestamp = System.currentTimeMillis();
        System.out.println("Starting at "+timestamp);
        System.out.println("1: "+test.getDistance());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("2: "+test.getDistance());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("waiting distance sensor and making 3 getDistance calls then sleeping main thread for 1 second - all 3 getDistance calls should be printed when the sleep ends");
        test.pauseDistanceSensor();
        System.out.println("3: "+test.getDistance());
        System.out.println("4: "+test.getDistance());
        System.out.println("5: "+test.getDistance());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Alive! Notifying the thread");
        test.resumeDistanceSensor();
        System.out.println("Done at "+System.currentTimeMillis());
    }
}

class DistanceSensor implements Runnable
{
    private final Random gen = new Random(54);
    private int currentVal;
    public DistanceSensor()
    {
        this.currentVal = this.gen.nextInt(1500);
    }

    public void run()
    {
        this.currentVal = this.gen.nextInt(1500);
    }

    public int getMeasurement()
    {
        return this.currentVal;
    }
}
  • 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-15T12:19:03+00:00Added an answer on May 15, 2026 at 12:19 pm

    Your call to the ‘pauseDistanceSensor’ blocks the main thread on the wait call.

    Also your sensor run method only sets the sensor value once; the run method should loop, setting the value each time.

    Your pause method should instead call a synchronized method that suspends the sensor run loop, using a boolean flag or similar.

    import java.util.Random;
    
    public class TestThreads
    {
        private DistanceSensor dist;
        private Thread distanceThread;
        public TestThreads()
        {
            this.dist = new DistanceSensor();
            this.distanceThread = new Thread(this.dist);
            this.distanceThread.start();
        }
    
        public int getDistance()
        {
            return this.dist.getMeasurement();
        }
    
        public void pauseDistanceSensor()
        {
          dist.setPaused(true);
        }
    
        public void resumeDistanceSensor()
        {
          dist.setPaused(false);
        }
    
        public void finish() {
          dist.setDone();
        }
    
        public static void main(String[] args)
        {
            TestThreads test = new TestThreads();
            long timestamp = System.currentTimeMillis();
            System.out.println("Starting at "+timestamp);
            System.out.println("1: "+test.getDistance());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("2: "+test.getDistance());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("waiting distance sensor and making 3 getDistance calls then sleeping main thread for 1 second - all 3 getDistance calls should be printed when the sleep ends");
            test.pauseDistanceSensor();
            System.out.println("3: "+test.getDistance());
            System.out.println("4: "+test.getDistance());
            System.out.println("5: "+test.getDistance());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Alive! Notifying the thread");
            test.resumeDistanceSensor();
            System.out.println("Done at "+System.currentTimeMillis());
            test.finish();
        }
    }
    
    class DistanceSensor implements Runnable
    {
        private final Random gen = new Random(54);
        private int currentVal;
        private boolean done = false, paused = false;
        public DistanceSensor()
        {
            this.currentVal = this.gen.nextInt(1500);
        }
    
        public void run()
        {
            while(!getDone()) {
              if(!getPaused()) synchronized(this) {this.currentVal = this.gen.nextInt(1500);}
              synchronized(this) {
                try {
                  wait(500);
                } catch(InterruptedException ex) {
                  ex.printStackTrace();
                }
              }
            }
        }
    
        public synchronized int getMeasurement()
        {
            return this.currentVal;
        }
    
        public synchronized boolean getPaused() {return paused;}
        public synchronized boolean getDone() {return done;}
        public synchronized void setPaused(boolean p) {paused = p;}
        public synchronized void setDone() {done = true;notify();}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code below, not sure what i'm doing wrong. It is an employee database. The
My code below seems logical however i don't know why the sorting doesn't work
Code below is not working as expected to detect if it is in design
code below working fine but not in IE6, IE7, below is the code is
The code below works for me. How could this work? Wouldn't this segfault? char
Code below doesn't work on any browser. It's supposed to show an alert box.
Code below is working well as long as I have class ClassSameAssembly in same
The code below simply didn't work. document.getElementById('files').addEventListener('change', handleFileSelect, false); reported by firebug that this
This code below allows me to find the word error in all my files
Code below. The outcome of this is that each document ends up with 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.