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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:16:12+00:00 2026-06-02T23:16:12+00:00

suppose we have these classes: public class Record { int key; int value; Record(){

  • 0

suppose we have these classes:

public  class Record {
    int key;
    int value;
    Record(){
        this.key=0;
        this.value=0;
    }
    Record(int key,int value){
        this.key=key;
        this.value=value;
    }
    public  class Table {
        static final Record[] table = new Record [100];
        static final Object[] locks = new Object[table.length];
        static{
        for(int i = 0; i < table.length; i++) {
            locks[i] = new Object();
        }


        table[0]=new Record(0,0);
        table[1]=new Record(1,10); 
        table[2]=new Record(2,20);
        table[3]=new Record(3,30);
    }
}

And i want to implement in TRansaction class these method

void setValueByID(int key, int value)

The key-value pair (record) is locked(it cannot be read/written from other transactions) until the setValueByID method finishes.

int getValueByID(int key)

The key-value pair (record) is locked until the transaction commits

void commit()

it unlocks all the key-value pairs (records) locked in the current transaction

So, my implementation is :

class Transaction extends Thread {
//there is no problem here  
    public void setValueByID(int key, int value){

    synchronized(Table.locks[key]) {
     Table.table[key].key=key;

    }   
    }
    //the problem is here...
    //how can i make other thread wait until current thread calls Commit()
    public int getValueByID(int key){
            int value=0; 
            synchronized(Table.locks[key]){
            value= Table.table[key].key;
     }
     return value;
} 


void commit(){
}

Ahmad

  • 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-02T23:16:16+00:00Added an answer on June 2, 2026 at 11:16 pm

    You cannot use synchronized blocks to achieve it, instead you will need to use something like locks.

    public Main {
        public static void main(String[] args) {
            final Transaction T = new Transaction();
            for (int n = 0; n < 10; n++) {
                new Thread(new Runnable() {
                    public void run() {
                        for (int i = 0; i < 1000; i++) {
                            T.setValueByID(i % 100, i);
                            T.getValueByID(i % 100);
                            if (i % 20 == 0) T.commit();
                        }
                    }  
                }).start();
            }
        }
    }
    class Table {
        static final Record[] table = new Record[100];
        static final ReentrantLock[] locks = new ReentrantLock[table.length];
    
        static {
            for (int i = 0; i < table.length; i++) {
                locks[i] = new ReentrantLock();
            }
    
            table[0] = new Record(0, 0);
            table[1] = new Record(1, 10);
            table[2] = new Record(2, 20);
            table[3] = new Record(3, 30);
        }
    }
    class Transaction {
    
        private ThreadLocal<Set<ReentrantLock>> locks = new ThreadLocal<Set<ReentrantLock>>() {
            @Override
            protected Set<ReentrantLock> initialValue() {
                return new HashSet<ReentrantLock>();
            }
        };
    
        private void attainLock(int key) {
            final ReentrantLock lock = Table.locks[key];
            lock.lock();
            locks.get().add(lock);
        }
    
        private void releaseLock(int key) {
            final ReentrantLock lock = Table.locks[key];
            releaseLock(lock);
        }
    
        private void releaseLock(ReentrantLock lock) {
            final Set<ReentrantLock> lockSet = locks.get();
            if (!lockSet.contains(lock)) {
                throw new IllegalStateException("");
            }
            lockSet.remove(lock);
            lock.unlock();
        }
    
        private void releaseLocks() {
            final Set<ReentrantLock> lockSet = new HashSet<ReentrantLock>(locks.get());
            for (ReentrantLock reentrantLock : lockSet) {
                releaseLock(reentrantLock);
            }
        }
    
        public void setValueByID(int key, int value) {
            attainLock(key);
            Table.table[key].key = key;
            releaseLock(key);
        }
    
        public int getValueByID(int key) {
            attainLock(key);
            return Table.table[key].key;
        }
    
        void commit() {
            releaseLocks();
        }
    }
    

    The problem with locks is that during your transaction, if you do not follow an order while attaining the locks, you can run into deadlocks! Also, you need to ensure you handle exceptions properly and always release the locks by calling commit().

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

Sidebar

Related Questions

Suppose you have 2 classes like so: public class ClassA { public int X
Suppose we have declared these two classes: public class Animal { //..... } public
Suppose I have the following classes: public class Test { public static void main(String[]
Suppose you have these tables: Table Name: Salesman Fields: S_ID(Primary Key), Name Table Name:
The scenario Suppose I have the following two model classes: public class ProductColor {
Suppose I have these two objects: public class Object1 { string prop1; string prop2;
I am a NHibernate newbie. Suppose you have a Customer class like this public
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Suppose I have these abstract classes Foo and Bar : class Foo; class Bar;
Suppose I have two classes deriving from a third abstract class: public abstract class

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.