简体   繁体   English

多个方法之间的Java同步

[英]java synchronization between multiple methods

class xyz {

    private Object lock = new Object();

    // value will come from database
    private boolean deleteOncePerDay; 

    private void clearAll() {
        synchronized (lock) {
            if(!deleteOncePerDay) {
                database.deleteNames();
                database.deleteAddresses();
                database.deletePhoneNums();

                // set database value to true
                deleteOncePerDay = true;
            }
        }   
    }

    @Scheduled(fixedDelay = 50000)
    public void fetchNames() {
        clearAll();
        synchronized (lock) {
            database.saveNames();
        }
    }

    @Scheduled(fixedDelay = 50000)
    public void fetchAddresses() {
        clearAll();
        synchronized (lock) {
            database.saveAddresses();
        }
    }

    @Scheduled(fixedDelay = 50000)
    public void fetchPhoneNums() {
        clearAll();
        synchronized (lock) {
            database.savePhoneNums();
        }
    }

}

The 3 fetch methods work in different threads and each fetch method take different times to save their data. 3种提取方法在不同的线程中工作,每种提取方法花费不同的时间来保存其数据。

My Objectives: 我的目标:

  1. When one thread is deleting (ClearAll) data, no other thread should save data simultaneously. 当一个线程正在删除(ClearAll)数据时,没有其他线程可以同时保存数据。

  2. When a thread is saving data, no other thread should be deleting data. 当一个线程正在保存数据时,其他任何线程都不应删除数据。

  3. When no threads are working on clearAll(), All threads can save data simultaneously. 当没有线程在clearAll()上运行时,所有线程可以同时保存数据。

With the above i guess i achieved 1 & 2. But i missed 3. since each fetch method is enclosed by synchronized block, only fetch method will get lock at time, so doesn't satisfy condition 3. 通过以上操作,我想我达到了1&2。但是我错过了3。由于每种获取方法都被同步块包围,因此只有获取方法会在时间锁定,因此不满足条件3。

Can someone help me? 有人能帮我吗?

In this case, you can use ReadWriteLock in java. 在这种情况下,可以在Java中使用ReadWriteLock Instead of synchronized(lock){} blocks, use that as follows. 代替使用synchronized(lock){}块,如下所述。

// Initialization
ReadWriteLock lock=new ReentrantReadWriteLock();

In the clearAll() method. clearAll()方法中。

lock.writeLock().lock();
try{
    // Do your stuff here
} finally{
    lock.writeLock().unlock();
}

In all the other methods, just get a read lock and do your stuff. 在所有其他方法中,只需获取读锁即可完成工作。

lock.readLock().lock();
try{
    // Do your stuff here
} finally{
    lock.readLock().unlock();
}

A ReadWriteLock allows any number of threads to get the read lock, but to get the write lock, all the read locks have to be released. ReadWriteLock允许任意数量的线程获取读取锁,但要获取写入锁,必须释放所有读取锁。 Furthermore, no read can happen when a write lock is taken. 此外,采取写锁定时不会发生任何读操作。

Also, I think you should look at database transactions where you can achieve all these at the database level . 另外,我认为您应该研究数据库事务,您可以在数据库级别实现所有这些事务

@Scheduled(fixedDelay = 50000)
    public void whatever() {
        clearWhatever(); // no one passed barrier, no saves happening
        CyclicBarrier();
        database.saveWhatever(); // all passed barrier, no clears happening.

    }

this should divide work into clear and save without concurrency problems according to your 1,2,3 cases. 根据您的1,2,3情况,这应将工作分为清晰并保存而不会出现并发问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM