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

  • Home
  • SEARCH
  • 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 8638739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:48:51+00:00 2026-06-12T10:48:51+00:00

My expected output is Count : 1 Count : 2 Count : 3 Count

  • 0

My expected output is

Count : 1
Count : 2
Count : 3
Count : 4
Count : 5

I have tried synchronized and Lock but neither of them works. I reach to

Ending Main

sooner than I finish the loop completely. Current output is:

Starting Main
Count : 1
Count : 2
Count : 3
Count : 4
Ending Main
Count : 5

Any Idea why Count : 5 is after Ending Main? Here is my code:

public class Demo {
    public static void main( String [] args ) {
        System.out.println( "Starting Main" ) ;
        for ( int i = 1 ; i <= 5 ; i++ ) {
            Thread numberThread = new Thread(new NumberTask(i)) ;
            numberThread.start() ;
        }
        System.out.println( "Ending Main" ) ; 
    }
}

class NumberTask implements Runnable {
    private Lock bankLock = new ReentrantLock();
    int count ;
    public NumberTask( int count ) {
        this.count = count ;
    }

    synchronized public void run() {
        bankLock.lock();
        try {
            System.out.println( "Count : " + count ) ;
        } finally {
            bankLock.unlock();
        }
    }
}
  • 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-12T10:48:52+00:00Added an answer on June 12, 2026 at 10:48 am

    You can use join() to wait for other thread to finish. Your code need to be updated as follows:

    public static void main( String [] args ) {
    
        System.out.println( "Starting Main" ) ;
            Thread numberThread;
            for ( int i = 1 ; i <= 5 ; i++ ) {
                numberThread = new Thread(new NumberTask(i)) ;
                numberThread.start() ;
            }
            numberThread.join();
            System.out.println( "Ending Main" ) ; 
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have summary of difference between two files. Expected output is count
This code when executed displays the expected output but prints segmentation fault (core dumped)
Expected output & output I get in debug mode, and release mode under VS2010,
My LINQ query is not producing the expected output below. Basically, it's the sum
$fr = hammad; $frhammad = nuthing; echo $fr{$fr}; Output: h Whereas the expected output
I expected to see the word test appear in the output once and the
I expected this to print [b] but it prints [] : $x = abc;
Tring to get C++ regex string capture to work. I have tried all four
Problem I have a Fetch XML query that has an aggregate count on a
I have the following xsl stylesheet: <xsl:stylesheet xmlns=http://www.w3.org/1999/xhtml xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:output method=xml encoding=utf-8/> <xsl:template match=/>

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.