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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:44:40+00:00 2026-05-31T01:44:40+00:00

everyone. I’ve written a synchronization code in java. There are 2 classes, the first

  • 0

everyone. I’ve written a synchronization code in java. There are 2 classes, the first one increases a number by 1000, the second one increses the same number by 10. The process should repeat itself 100 times.
Although I’ve written loop cycles they don’t work.

Here is the code:

public class thread
{
  static int count = 100;
  public static void main(String[] args) 
  {
    Thread thread1 = new Thread(new XThread());
    Thread thread2 = new Thread(new YThread());

    thread1.start();
    thread2.start();
    synchronized(thread2)
      {
        thread2.notify();
      }

  }
}
class XThread extends Thread {
  static long sum=0;

  static int i;
  synchronized public void run() {

    sum=5+1000;
    System.out.println(i+" "+"Thread 1"+" "+sum);

    {
      for(i= 0; i < lab5.count; i++)
        {
          try {
            {
              System.out.println("-----------");                
              this.wait();
            }



          } 
          catch (InterruptedException ex) 
            {
              sum=sum+1000;
              System.out.println(i+" "+"Thread 1"+" "+sum);
              notify();

            }
        }
    }

  }
}

class YThread extends Thread 
{
  static long sum;
  static int i;
  synchronized public void run() 
  {
    sum=5+10;
    System.out.println(YThread.i+" "+"Thread 2"+" "+YThread.sum);
    for(i=0; i < lab5.count; i++)
      {

        try 
          {
            {
              System.out.println("------------");
              this.wait();
            }

          } 

        catch (InterruptedException ex) 
          {
            sum=sum+10;
            System.out.println(YThread.i+" "+"Thread 2"+" "+YThread.sum);
            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-05-31T01:44:41+00:00Added an answer on May 31, 2026 at 1:44 am

    In your code you are calling wait in run method of your Thread overrides.

    Unfortunately, nobody calls notify method on the object that is waiting.

    This is because in your main, you re-wrapped your X and YThread into generic Thread object.

    Because of this a call to thread2.notify() sends the signal to the wrong object ( not the YThread that is locked, but the wrapper object ).

    Change the declaration to

    Thread thread1 = new XThread();
    Thread thread2 = new YThread();
    

    Your program still going to lock ( because you are not notifying threads enough times ), but at least it may get further. Notice may in my last sentence. This is because the notify to thread2 may be sent before the thread calls its first wait.

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

Sidebar

Related Questions

everyone. How do I call a number of forms one after another (meaning call
Everyone knows there is a free source code application available for iPad named ComicFlow,
Everyone uses source-code control to manage versions (right?) and this provides some level of
Everyone's saying Contract-First approach to design WS is more inclined to SOA style design.
everyone. I used openjdk-7 on arch linux. I started to learn Java recently, and
everyone The problem is if there exists two exported components which have the same
Everyone knows that Java supports data hiding. I went for an interview. Then interviewer
everyone. I'm new here and this is my first post. I'm creating a simple
Everyone knows public class Test<T> where T : SomeBaseClass But is there a way
everyone, I have this piece of the code: void foo(int var, int var1) {

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.