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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:32:29+00:00 2026-05-24T12:32:29+00:00

If I have a synchronized method and two threads are waiting to enter it

  • 0

If I have a synchronized method and two threads are waiting to enter it they seem to enter the thread Last In First Executed. Is there a way to make this First In First Executed?

This is the unit test that I’m using:

package com.test.thread;

import org.apache.log4j.Logger;
import org.junit.Test;

public class ThreadTest {
    private static final Logger log = Logger.getLogger(ThreadTest.class);

    @Test
    public void testThreading() throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {    
            public void run() { synchd("1"); }
        });
        Thread t2 = new Thread(new Runnable() {    
            public void run() { synchd("2"); }
        });
        Thread t3 = new Thread(new Runnable() {    
            public void run() { synchd("3"); }
        });

        t3.start();
        Thread.sleep(5);
        t1.start();
        t2.start();

        Thread.sleep(12000);
    }

    public static synchronized void synchd(String output) {
        log.debug(output);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // do nothing
        }

    }
}

The output for this is always 3, 2, 1, and I’d like to find a way for it to be 3, 1, 2.

  • 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-24T12:32:30+00:00Added an answer on May 24, 2026 at 12:32 pm

    With java.util.concurrent.locks.ReentrantLock, you can specify a fairness policy that’s relevant here. Passing true to the referenced constructor requests that the lock “play fair.” What that means is a little vague in the documentation, but studying the documentation for the underlying AbstractQueuedSynchronizer type and the implementation of ReentrantLock$FairSync (in the Sun JDK) gives additional hints:

    Namely, when one thread attempts to acquire the lock in fair mode, it will not “barge” ahead of other waiting threads; if any other threads are waiting for the lock, the newly arriving thread will “get it line.”

    Now, it’s still possible—though highly unlikely—that this newly-queued thread will acquire the lock next when it’s next released by its current holder, if it by then happens to wind up as first in line due to predecessors being interrupted, but observe that even though the logical model for a condition queue is a set of waiting threads, in the aforementioned implementation it is in fact a queue (a CLH Queue, described in its application to the Java library in the paper The java.util.concurrent Synchronizer Framework). In the fair mode, only the first item in the queue will acquire the lock.

    Obviously, two threads can race when trying to acquire the lock “at the same time.” With a fair lock, you can expect that if thread A arrives first and calls Lock#lock(), and winds up having to wait because the lock is held by thread C, and later thread B arrives and calls Lock#lock() while thread C still holds it, B will get in line behind the already-queued A, and once C releases the lock, A will get a chance before B to acquire it. See the implementation in AbstractQueuedSynchronizer$unparkSuccessor() for the specific walk forward from the CLH queue’s head toward its tail. A will be closer to the head than B.

    The documentation for ReentrantLock warns that even when operating in fair mode, it’s possible that threads already waiting on the lock will lose out to the thread currently holding the lock releasing it and acquiring it again. I think—but am not sure—that this can occur when the current thread wins at getting in line ahead of other threads that have not yet landed in the queue. Also, note the warning concerning ReentrantLock#tryLock(); unlike the timed ReentrantLock#tryLock(long, TimeUnit), the former does not honor the fairness policy.

    This survey makes some conclusions based on one implementation. In general, it’s safer to take Mr. Barousse’s view: the acquisition ordering is best thought of as being a random grab from a set of waiters. However, with a fairness policy studied in enough depth, you can see that there is some determinism to be had. It doesn’t come for free, though; note the warnings about decreases in throughput when barging is prohibited.

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

Sidebar

Related Questions

we have two threads accessing one list via a synchronized method. Can we a)
I have two threads, one thread processes a queue and the other thread adds
What is the best way to have synchronized a collection of objects between various
I have two threads, and I want to make sure I am doing the
I have a method and a thread which I'd like to run in the
suppose we have multi processor machine and multi threaded application. If two threads have
I have thread BLOCK issue. Please help me. In my class, i have two
Let say I have the following method, is the method thread safe? public static
I have a great deal of data to keep synchronized over 4 or 5
Have you ever seen any of there error messages? -- SQL Server 2000 Could

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.