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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:48:52+00:00 2026-06-07T08:48:52+00:00

Ok, so can some one explain to me the gap in my knowledge here?

  • 0

Ok, so can some one explain to me the gap in my knowledge here?

Initially the example below was trying to synchronize an instance method, but then realised that I spawn a new instance and therefore a lock wouldn’t happen.

So I decided to make a lock on a static method of the class in the hope that the thread would then run in order but still no luck. Can any explain the error of my ways? ( Bear with me there are probably better ways to do this its just getting the understanding right, I’m a PHP developer going into Java, I love it – but I’m only 2 days in 😉 )

So at this time the numbers print out in a random order.

Class 1

package learningjava;


public class LearningJava {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

      ThreadCaller ob1 = new ThreadCaller("This is a test string 1");
      ThreadCaller ob2 = new ThreadCaller("This is a test string 2");
      ThreadCaller ob3 = new ThreadCaller("This is a test string 3");
      ThreadCaller ob4 = new ThreadCaller("This is a test string 4");
      ThreadCaller ob5 = new ThreadCaller("This is a test string 5");

      try {

           ob1.t.join();
           ob2.t.join();
           ob3.t.join();
           ob4.t.join();
           ob5.t.join();

      } catch (InterruptedException e) {

          System.out.println(e);

      }

    }    

}

Class 2

package learningjava;

public class ThreadCaller implements Runnable {

    private String message;
    public Thread t;

    public ThreadCaller(String text) {
        message = text;
        t = new Thread(this);
        t.start();
    }

    public static synchronized void echo(String message) {
        System.out.println(message);
    }


    public void run() {
            ThreadCaller.echo(this.message);
    }

}
  • 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-07T08:48:53+00:00Added an answer on June 7, 2026 at 8:48 am

    Can any explain the error of my ways?

    Basically, you’re expecting ordering which simply isn’t guaranteed. You’re calling start() on lots of threads in succession… there’s no guarantee about which one will actually start executing first. The fact that you’ve got a static synchronized method just means that only one thread will be executing that method at a time – it doesn’t guarantee anything about ordering.

    Imagine you have a running track with a one-lane gate 100m down the track. You start the race – which runner will reach the gate first?

    Fortunately, this isn’t usually a problem – if it’s worth starting multiple threads to do something, you usually don’t care about the order in which they execute. If you do, it’s time to re-examine your design.

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

Sidebar

Related Questions

Please can some one explain the javacv method for the pyrDown() method. I'm trying
Can some one explain the below, rather complex recursive generic template usage? public abstract
Can some one explain to me why the GetProperties method would not return public
Can some one explain to me the private constructor capture idiom with an example
Can some one explain (or quote a reference) to compare the scoring mechanism used
Can some one please explain why this loop never 'breaks' and goes on forever
Can any one explain difference between position and anchor point in cocos-2D with some
Can some one explain Given a hash table array of size N=11., and using
can some one explain the difference between Team Build + MSBuild ? also how
when I type wget http://yahoo.com:80 on unix shell. Can some one explain me what

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.