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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:10:55+00:00 2026-06-10T11:10:55+00:00

Following code found that output results is not the order, not from small to

  • 0

Following code found that output results is not the order, not from small to large, how to guarantee it is order from small to large?

java code

public class TestSync {  

    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        for (int i = 0; i < 10; i++) {  
            new Thread(new Thread1()).start();  
        }  

    }  

    public int getNum(int i) {  
        synchronized (this) {  
            i++;  
        }  
        return i;  
    }  

    static class Thread1 implements Runnable {  
        static Integer value = 0;  
        @Override  
        public void run() {  
            TestSync ts = new TestSync();  
            value = ts.getNum(value);  
            System.out.println("thread1:" + value);  
        }  
    }  

}  
  • 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-10T11:10:57+00:00Added an answer on June 10, 2026 at 11:10 am

    What are you trying to accomplish? Your code is only synchronizing calls made to a particular TestSync instance. Since each thread creates its own instance, it’s like you are not synchronizing anything at all. Your code is not doing anything to synchronize or coordinate accesses across the different threads.

    I’d suggest the following code may be more along the lines of what you are trying to accomplish:

    public static void main (String[] args) throws java.lang.Exception {
            for (int i = 0; i < 10; i++) {  
                new Thread1().start();  
            }  
    }
    
    //no need for this to be an instance method, or internally synchronized
    public static int getNum(int i) {  
           return i + 1;  
    }
    
    static class Thread1 extends Thread {  
        static Integer value = 0;  
    
        @Override  
        public void run() {  
            while (value < 100) {
                synchronized(Thread1.class) {  //this ensures that all the threads use the same lock
                    value = getNum(value);  
                    System.out.println("Thread-" + this.getId() + ":  " + value);  
                }
    
                //for the sake of illustration, we sleep to ensure some other thread goes next
                try {Thread.sleep(100);} catch (Exception ignored) {} 
            }
        }  
    }
    

    Live example here: http://ideone.com/BGUYY

    Note that getNum() is essentially superfluous. The example above would work the same if you replaced value = getNum(value); with a simple value++;.

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

Sidebar

Related Questions

I am trying to run the following code that I found marked as correct
The following code can be found in the NHibernate.Id.GuidCombGenerator class. The algorithm creates sequential
Hi I found the following code from this page JQuery UI DatePicker using 2
I found the following code somewhere, but I am not understanding the code properly.
I found the following code in my team's project: Public Shared Function isRemoteDisconnectMessage(ByRef m
The following code taken from: Java code for using google custom search API .
I've found the following code: https://github.com/roddi/ValidateStoreReceipt/blob/master/validatereceipt.m which loads the root certificate ('Apple Root CA')
The following code produces no suitable constructor found error. I am unable to figure
I found a very interesting question. When I'm using following code: int main() {
At a website , I found the following code to make a jQuery plugin:

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.