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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:51:19+00:00 2026-06-07T22:51:19+00:00

I know that there are a few threads open regarding this topic, but I’m

  • 0

I know that there are a few threads open regarding this topic, but I’m just looking for a VERY ELEMENTARY example of how to use wait() and notify() in Java. By "VERY ELEMENTARY," I mean simply printing something out. Thanks.

EDIT: Here’s what I have tried thus far and I get an IllegalMonitorStateException:

public void waiting() {
        for(int i = 0; i < 10; i++) {
            if(i == 5)
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    
                }
            else
                System.out.println(i);
        }
        System.out.println("notify me now");    
        this.notify();
}
  • 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-07T22:51:22+00:00Added an answer on June 7, 2026 at 10:51 pm

    wait and notify are used in synchronized block while using threads to suspend and resume where left off.

    Wait immediately looses the lock, whereas Nofity will leave the lock only when the ending bracket is encountered.

    public class Mythread implements Runnable{
    
    
    
    public synchronized void goo(){
    
    System.out.println("Before Wait");
    
    wait();
    
    System.out.println("After Wait");
    
    
    }
    
    
    public synchronized void foo(){
    
    System.out.println("Before Notify");
    
    notify();
    
    System.out.println("After Notify");
    
    }
    
    
    public class Test{
    
    public static  void main(String[] args){
    
    Thread t = new Thread(new Mythread);
    
    t.start();
    
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been a few threads on this topic but none have been able
I know there have been a few threads on this before, but I have
I know there are a few answers on this topic on SO, but I
I know that there are some threads have a similar issue with this thread.
I know that there are lot's of questons on this, but all seem to
I know that there are lots of examples out there on this, but they
I know there's tons of threads about this. And I read a few of
I know that there are a few similar questions and I googled a lot
I know that there are others posts with solutions on the site but i
I know that there can be multiple values for an email, but I'm not

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.