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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:01:32+00:00 2026-05-12T07:01:32+00:00

In multi-thread environment, in order to have thread safe array element swapping, we will

  • 0

In multi-thread environment, in order to have thread safe array element swapping, we will perform synchronized locking.

// a is char array.
synchronized(a) {
    char tmp = a[1];
    a[1] = a[0];
    a[0] = tmp;
}

Is it possible that we can make use of the following API in the above situation, so that we can have a lock free array element swapping? If yes, how?

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.html#compareAndSet%28T,%20V,%20V%29

  • 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-12T07:01:32+00:00Added an answer on May 12, 2026 at 7:01 am

    Regardless of API used you won’t be able to achieve both thread-safe and lock-free array element swapping in Java.

    The element swapping requires multiple read and update operations that need to be performed atomically. To simulate the atomicity you need a lock.

    EDIT:

    An alternative to lock-free algorithm might be micro-locking: instead of locking the entire array it’s possible to lock only elements that are being swapped.

    The value of this approach fully is questionable. That is to say if the algorithm that requires swapping elements can guarantee that different threads are going to work on different parts of the array then no synchronisation required.

    In the opposite case, when different threads can actually attempt swapping overlapping elements then thread execution order will matter. For example if one thread tries to swap elements 0 and 1 of the array and the other simultaneously attempts to swap 1 and 2 then the result will depend entirely on the order of execution, for initial {‘a’,’b’,’c’} you can end up either with {‘b’,’c’,’a’} or {‘c’,’a’,’b’}. Hence you’d require a more sophisticated synchronisation.

    Here is a quick and dirty class for character arrays that implements micro locking:

    import java.util.concurrent.atomic.AtomicIntegerArray;
    
    class SyncCharArray {
    
        final private char array [];
        final private AtomicIntegerArray locktable;
    
        SyncCharArray (char array[])
        {
          this.array = array;
    
          // create a lock table the size of the array
          // to track currently locked elements 
          this.locktable = new AtomicIntegerArray(array.length);
          for (int i = 0;i<array.length;i++) unlock(i);
    
        }
    
        void swap (int idx1, int idx2)
        {
          // return if the same element
          if (idx1==idx2) return;
    
          // lock element with the smaller index first to avoid possible deadlock
          lock(Math.min(idx1,idx2));
          lock(Math.max(idx1,idx2));
    
          char tmp = array[idx1];
          array [idx1] = array[idx2];
          unlock(idx1);
          array[idx2] = tmp;
          unlock(idx2);
    
        }
    
        private void lock (int idx)
        {
          // if required element is locked when wait ...
          while (!locktable.compareAndSet(idx,0,1)) Thread.yield();
        }
    
        private void unlock (int idx)
        {
          locktable.set(idx,0);
        }
    
    }
    

    You’d need to create the SyncCharArray and then pass it to all threads that require swapping:

    char array [] = {'a','b','c','d','e','f'};
    SyncCharArray sca = new SyncCharArray(array);
    
     // then pass sca to any threads that require swapping
     // then within a thread
    
    sca.swap(15,3);
    

    Hope that makes some sense.

    UPDATE:

    Some testing demonstrated that unless you have a great number of threads accessing the array simulteniously (100+ on run-of-the-mill hardware) a simple synchronise (array) {} works much faster than the elaborate synchronisation.

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

Sidebar

Ask A Question

Stats

  • Questions 204k
  • Answers 204k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use TOP n, but you have to put… May 12, 2026 at 8:46 pm
  • Editorial Team
    Editorial Team added an answer LINQ is very good at running operations against collections of… May 12, 2026 at 8:46 pm
  • Editorial Team
    Editorial Team added an answer Date is almost entirely deprecated and is still there for… May 12, 2026 at 8:46 pm

Related Questions

I am working on a multi-threaded application. This application started out as a single
Why is the size of files capped at 4 GB when outputting to a
Assume a multi-threaded environment and a (properly synchronized) class that has one particular procedure
In a multi-server environment, users will be able to use a page to put,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.