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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:28:45+00:00 2026-06-07T13:28:45+00:00

Here is a code i am having problem with — public class WaitTest {

  • 0

Here is a code i am having problem with —

public class WaitTest {
    public static void main(String[] args) {
        Runner rr = new Runner();
        Thread t1 = new Thread(rr,"T1");
        Thread t2 = new Thread(rr,"T2");
        t1.start();
        t2.start();
    }
}

class Runner implements Runnable{
    int i=0;
    public void run(){
        try{
            if(Thread.currentThread().getName().equals("T1")){
                bMethod();
                aMethod();
            }
            else{
                aMethod();
                bMethod();
            }
        }catch(Exception e){}
    }

    public synchronized void aMethod() throws Exception{
        System.out.println("i="+i+",Now going to Wait in aMethod "+Thread.currentThread().getName());
        Thread.currentThread().wait();
        System.out.println("Exiting aMethod "+Thread.currentThread().getName());
    }

    public synchronized void bMethod() throws Exception{
        System.out.println("i="+i+",Now going to Wait bMethod "+Thread.currentThread().getName());
        i=5;
        notifyAll();
        System.out.println("Exiting bMethod "+Thread.currentThread().getName());
    }
}

The output is :

i=0,Now going to Wait bMethod T1
Exiting bMethod T1
i=5,Now going to Wait in aMethod T1
i=5,Now going to Wait in aMethod T2

My question is :

Why T2 enters in aMethod while T1 is waiting inside? and Why T2 prints
i=5 in aMethod.

  • 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-07T13:28:47+00:00Added an answer on June 7, 2026 at 1:28 pm

    When you execute wait, your thread releases the lock and enters the wait state. At this time the other thread is allowed to enter the method. You are using a single instance of Runnable so when one thread sets it to 5, that’s what the other thread reads.

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

Sidebar

Related Questions

I'm having a problem with this code here. using (MemoryStream ms = new MemoryStream())
Im having this problem when changing my entities connection string. here is the code:
I' having a problem allocating a structure in a function. Here is the code(I'm
What is the problem here? (Besides having redundant code). $.getJSON works as expected. However
Here is the code I'm having problems with: The interface: public interface anInterface {
Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
I am having problem creating a new directory inside my FTP using mkdir, here
I'm having a problem with the Auto complete plug-in. Here is my code: var
I'm having a problem to get value selected for spinner. here is my code
I'm having a small problem trying to malloc this struct. Here is the code

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.