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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:53:36+00:00 2026-05-23T10:53:36+00:00

I was given following code snippet: public class ThreadTest{ private static class Thread01 extends

  • 0

I was given following code snippet:

public class ThreadTest{

  private static class Thread01 extends Thread{
    private Thread02 _th2; 
    public int foo = 0;

    public void setThrd02(Thread02 thrd2){
       _th2 = thrd2;
    }

    public void run(){
      try{
        for(int i=0;i<10;i++) foo += i;

        synchronized(this){this.notify();};

        synchronized(_th2){_th2.wait();};

        System.out.print(" Foo: " + _th2.foo);
      }catch(InterruptedException ie){ ie.printStackTrace();}    
    }
  }

  private static class Thread02 extends Thread{

    private final Thread01 _th1;
    public int foo = 0;

    public Thread02(Thread01 th1){
      _th1 = th1;
    }

    public void Run(){
       try{
         synchronized(_th1){_th1.wait();}
         foo = _th1.foo;

         for(int i=0;i<10;i++) foo += i;
         synchronized(this){this.notify();};
           }
       catch(InterruptedException ie){ie.printStackTrace();}

    }

  }

  public static void main(){
    Thread01 th1 = new Thread01();
    Thread02 th2 = new Thread02(th1);

    th1.setThrd02(th2);

    th1.start(); th2.start();
    th1.join(); th2.join();
  } 
}

I think the assumption and corresponding purpose of the code is like
th2 run first, it is changed to waiting status by calling _th1.wait();
Then, th1 calculates foo and wake up th2, th1 goes into waiting status;
Th2 reads foo from thread1 and updated to 110, then wakes up th1 and th2 exit.
Then th1 exit.

The threads could be very risk because it is very possible that thread one runs first and thread 2 will wait forever.

I am not sure any other potential problems of the code.

One possible way that can fix the problem is, for example in the thread1

public class ThreadTest{

private static boolean updated = false;
private static boolean finished = false;

private static Thread01 extends Thread{

public void Run(){
// do calcuation
while(finished){
wait();
}
// output result
}
}

private static Thread02 extends Thread{
public void run(){

while(false){
wait();
}

foo = th1.foo;
// do calculation
// similar mechanism to notify thread 1
}
}

  • 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-23T10:53:36+00:00Added an answer on May 23, 2026 at 10:53 am

    There is no guarantee of ordering in your threads. It’s sufficient for Thread01 to go past
    synchronized(this){this.notify();}; before Thread02 does synchronized(_th1){_th1.wait();} to have both threads waiting indefinitely.

    Note: The fact that you are calling wait and notify on _th1 and _th2 is irrelevant. Threads here will be treated as any other object.

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

Sidebar

Related Questions

I'm using EF 4.1 code first. Given the following class snippet: public class Doctor
Given the following code snippets, is there any appreciable difference? public boolean foo(int input)
Question: Given the following code snippet: bool foo(int n) { for(int i=3;i<sqrt(n)+0.5;i+=2) { if((n%i)==0){
Hey I am trying to understand the following code snippet. public static void main(String[]
Hi I have the following Code Snippet; class StringCalci { static def plus(Integer self,
Given the following code snippet: int[] arr = {1, 2, 3}; for (int i
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath
Given the following code snippet: $i= 11; function get_num() { global $i; return (--$i
Given the following code, is there a way I can call class A's version
I have seen the following code: [DefaultValue(100)] [Description(Some descriptive field here)] public int MyProperty{...}

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.