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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:29:18+00:00 2026-06-18T14:29:18+00:00

could you please check the below code, which is digital clock without util.concurrent package,

  • 0

could you please check the below code, which is digital clock without util.concurrent package, Calendar by using the sleep, notify, wait. the code is executing correctly please suggest any better way. Thanks alot

/**
 * 
 */
package com.nagihome.clock;

/**
 * @author Nagi
 * 
 */
public class ThreadAlarmClock {

    private static Integer hours = 23;
    private static Integer minutes = 59;
    private static Integer seconds = 55;
    private static Integer minSecLastNum = 60;
    private static Integer hour24LastNum = 24;
    private static String COLON = ":";
    private static Boolean isMinUpdatable = false;
    private static Boolean isHourUpdatable = false;
    final static Object lock = new Object();

    public void updateSeconds() throws InterruptedException {

        synchronized (lock) {
            while (true) {
                if (isMinUpdatable || isHourUpdatable) {
                    lock.wait();
                } else {
                    Thread.sleep(1000);
                    seconds++;
                    if (seconds.equals(minSecLastNum)) {
                        seconds = 0;
                        isMinUpdatable = true;
                        lock.notifyAll();
                    } else {
                        displayTime();
                    }
                }
            }
        }
    }

    public void updateMinutes() throws InterruptedException {

        synchronized (lock) {
            while (true) {
                if (!isMinUpdatable) {
                    lock.wait();
                } else if (isMinUpdatable) {
                    minutes++;
                    isMinUpdatable = false;
                    if (minutes.equals(minSecLastNum)) {
                        minutes = 0;
                        isHourUpdatable = true;
                    } else {
                        displayTime();
                    }
                }

                lock.notifyAll();
            }
        }
    }

    public void updateHours() throws InterruptedException {

        synchronized (lock) {
            while (true) {
                if (!isHourUpdatable) {
                    lock.wait();
                } else if (isHourUpdatable) {
                    hours++;
                    isHourUpdatable = false;
                    if (hours.equals(hour24LastNum)) {
                        hours = 0;
                    }

                    displayTime();
                }

                lock.notifyAll();
            }
        }
    }

    public void displayTime() {
        System.out.println(hours + COLON + minutes + COLON + seconds);
    }

}


/**
 * 
 */
package com.nagihome.clock;

/**
 * @author Nagi
 * 
 */
public class SecondsThread implements Runnable {

    private ThreadAlarmClock clock;

    public SecondsThread(ThreadAlarmClock clock) {
        this.clock = clock;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            clock.updateSeconds();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


/**
 * 
 */
package com.nagihome.clock;

/**
 * @author Nagi
 * 
 */
public class MinutesThread implements Runnable {

    private ThreadAlarmClock clock;

    public MinutesThread(ThreadAlarmClock clock) {
        this.clock = clock;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            clock.updateMinutes();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


/**
 * 
 */
package com.nagihome.clock;

/**
 * @author Nagi
 * 
 */
public class HoursThread implements Runnable {

    private ThreadAlarmClock clock;

    public HoursThread(ThreadAlarmClock clock) {
        this.clock = clock;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            clock.updateHours();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


/**
 * 
 */
package com.nagihome.main;

import com.nagihome.clock.HoursThread;
import com.nagihome.clock.MinutesThread;
import com.nagihome.clock.SecondsThread;
import com.nagihome.clock.ThreadAlarmClock;

/**
 * @author Nagi
 * 
 */
public class Main {

    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
        ThreadAlarmClock clock = new ThreadAlarmClock();

        new Thread(new SecondsThread(clock)).start();
        new Thread(new MinutesThread(clock)).start();
        new Thread(new HoursThread(clock)).start();         
    }

}
  • 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-18T14:29:20+00:00Added an answer on June 18, 2026 at 2:29 pm

    Suggestions:

    • Use internally milliseconds to count, not sec++
    • sleep(1000) does not exatcly sleep 1000 millis, it can be 990′ or 1110. so after waking up you check the system time (or nanoTime), and calculate the difference to last wake up time. If for example you detect that you have slept 1010 millis, then next time you sleep only 990 millis.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could you please check out this piece of code: #include <vector> class A {
Update : I posted my code solution as an answer down below, which could
Could anyone please tell me what went wrong for below code? I am trying
Please could someone post an example of how to check if an element exists
Could someone please suggest why this is happening... I’ve got some code to pretty
I have written below code to check for blank value in my textbox but
Could you please check and let me know what is the problem in the
the code below was written to check for NaN values in a Python ND-Array
Could anyone please explain how can I check if String is null or empty?
Quick question. I got a code below, which checks with a database whether a

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.