0

Java. I have two threads. one will be continuously monitoring for some events and based on the events, it will be updating (addition or deletion) a file. the other thread which is a timer task event, will be updating the same file in regular intervals of time. I want the threads to update the file when the other thread is not accessing the file. I couldn't use locks as file updating code part is independent for each thread and in different java classes. how can i do so? thanks in advance.

7
  • you need to create a class responsible for updating the file, and use synchronized for the methods you need to lock, check Siva Kumar's answer below Commented Dec 15, 2014 at 7:09
  • 1
    I don't understand your requirement not to use locks. You need to have some sort of synchronization between threads or shared resources if you do not want to have multiple threads writing to the same file, which will almost certainly use locking. Commented Dec 15, 2014 at 7:19
  • To add to @MikeKobit 's comment, if you are using BufferedReader / BufferedWriter, then you will be implicitly using locks. the read() and write() methods use synchronization and lock on the passed Reader / Writer. Any particular reason for not wanting to use locks? Commented Dec 15, 2014 at 7:21
  • @MikeKobit just to for the record, OP did not say he don't want to use locks, OP said i could not use locks, maybe a bad code design caused that. Commented Dec 15, 2014 at 7:50
  • @Yazan, say if I use synchronized for the reading method, i'm well aware that no two threads can read from the file at the same time but while a thread is reading from the file, the other thread can write in the file right? Commented Dec 15, 2014 at 8:24

3 Answers 3

1

You can use synchronization.

public synchronized void update() { .. .. .. } 

so only one thread access the method. Other will wait on the lock.

If you have add,update,delete method then,

private static Object LOCK = new Object(); public void update() { synchronized(LOCK) { .. .. .. } } public void add() { synchronized(LOCK) { .. .. .. } } public void delete() { synchronized(LOCK) { .. .. .. } } 

So Only one thread can able to edit/delete/add the file.

if one thread is adding , second thread is trying to delete then it will wait first thread to add.

Sign up to request clarification or add additional context in comments.

4 Comments

If i use synchronized to add method, no two threads can add data to the file at the same time but a thread can be adding data to the file while the other thread can be deleting at the same time right?
If you have separate method for add and delete then you can use synchronized block.
thanks @SivaKumar. In between, how about reentrant locks? which one is safer?
0
synchronized void updateFILE { //Your operation here } 

So at a time one thread can perform opetation.. You can have look at here

Comments

0

Perhaps you could:

  1. Create a wrapper class around your unsafe file-updater-class.
  2. Make that wrapper class thread-safe by adding your synchronization or locks that clearly defines the critical sections and handle all exceptions appropriately (for example, unlock the critical section in a finally-block)
  3. Let the other threads call your wrapper class instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.