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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:26:04+00:00 2026-05-16T11:26:04+00:00

I have main thread which calls another thread. timeout period of second one is

  • 0

I have main thread which calls another thread. timeout period of second one is 15 seconds. Current implementaion is main thread check second one for every 2 seconds. What I need to do is wait for only as long as second thread finish, upto a maximum of 15 seconds. I thought of trying wait and notify, but it will require synchronized. I am thinking of using Lock. I havent tried it before. Please help me on this.

Thanks

This idea can be implemented? by using timedwait on a lock object in the first thread. The second thread once done should notify on the lock object. This will wake up the first thread. Also, timedwait will force a max wait of 15 seconds.

Updated the solution, I tried.

public class MyThread {

Object obj = new Object();
boolean isDone = false;
int timeOut = 0;
int runTime = 0;    
int id = 0;

public static void main(String[] args) throws Exception {       
    MyThread mainThread = new MyThread();

    mainThread.timeOut = Integer.valueOf(args[0]);
    mainThread.runTime = Integer.valueOf(args[1]);
    System.out.println("<---- mainThread.timeOut ---------->" + mainThread.timeOut);
    System.out.println("<---- mainThread.runTime ---------->" + mainThread.runTime);
    ChildThread childThread = new ChildThread(mainThread);
    childThread.start();        
    try {
        synchronized (mainThread.obj) {
            //Wait the current Thread for 15 seconds
            mainThread.obj.wait(mainThread.timeOut * 1000);
        }       
        System.out.println("<---- runTime Over ---------->");
        if (mainThread.isDone)
            System.out.println("<---- done ---------->");
        else
            System.out.println("<----  not done ---------->");
    } catch (InterruptedException e) {          
        e.printStackTrace();
    }
}

public void test() {        
    System.out.println("<--- In business Test method --->");    
    try {
        //Call the bussiness method which may take more time
        Thread.sleep(runTime * 1000);
        if (runTime <= timeOut)
            isDone = true;
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("<---- completes business test method -->");
}
}

class ChildThread extends Thread {          
    MyThread mainThread;
    ChildThread(MyThread mainThread) {
        this.mainThread = mainThread;
    }

    public void run() { 
        System.out.println("ChildThread: the run method");
        mainThread.test();
        //Finished the work,notify the waiting thread.
        synchronized(mainThread.obj) {
            mainThread.obj.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-16T11:26:04+00:00Added an answer on May 16, 2026 at 11:26 am

    I have updated the question with answer.

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

Sidebar

Related Questions

No related questions found

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.