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.

Arun
  • 21
  • 1
  • 6
  • 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 – Yazan Dec 15 '14 at 07: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. – mkobit Dec 15 '14 at 07: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*? – TheLostMind Dec 15 '14 at 07: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. – Yazan Dec 15 '14 at 07: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? – Arun Dec 15 '14 at 08:24
  • @TheLostMind, I'm not using BufferedReader but I'm using MappedByteBuffer. It's not that I don want to use locks, I couldn't. It's done in different classes as I have mentioned earlier. – Arun Dec 15 '14 at 08:26
  • @Arun well, that's true, also `Siva Kumar's` have made another edit to the answer, i think this will lock all methods if 1 thread is using any of them. – Yazan Dec 15 '14 at 08:44

3 Answers3

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.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
  • 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? – Arun Dec 15 '14 at 08:38
  • If you have separate method for add and delete then you can use synchronized block. – Siva Kumar Dec 15 '14 at 08:40
  • thanks @SivaKumar. In between, how about reentrant locks? which one is safer? – Arun Dec 15 '14 at 08:56
  • @Arun http://javarevisited.blogspot.in/2013/03/reentrantlock-example-in-java-synchronized-difference-vs-lock.html Pl see – Siva Kumar Dec 15 '14 at 09:01
0
synchronized  void updateFILE
{
       //Your operation here
}

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

Rohit
  • 2,646
  • 6
  • 27
  • 52
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.
Jasper Citi
  • 1,673
  • 1
  • 23
  • 31