Resource conflict

Hi,

I have a program in which two threads, reading from the same file, run asynchronously. The problem is that sometime the two processes try to read the file together, so is thrown an exception. I think I need something like a semaphore, a flag telling us whether the file is busy or not.

Can you help me?

[325 byte] By [IronBargiaa] at [2007-11-26 22:53:58]
# 1

I'd want that before a thread start reading the file, it checks whether the semaphore is "green" or not. If it is green, it start to read the file and put the semaphore in "red" state, to prevent that the file is read by other threads; if it is "red", it wait until it becomes "green" before starting the reading.

IronBargiaa at 2007-7-10 12:17:32 > top of Java-index,Java Essentials,Java Programming...
# 2
Hello,Maybe you schould try synchronizing because the two threads share the same resource...tutorial : http://www.javaworld.com/jw-04-1996/jw-04-synch.htmlRegards,Tommie.
Tommie.Denoblea at 2007-7-10 12:17:32 > top of Java-index,Java Essentials,Java Programming...
# 3
Using a synchronized method also works.But if you really want Semaphoreyou can have a look on http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Semaphore.html
rym82a at 2007-7-10 12:17:32 > top of Java-index,Java Essentials,Java Programming...
# 4
Maybe, have you got an example code to show the usage of semaphores?
IronBargiaa at 2007-7-10 12:17:32 > top of Java-index,Java Essentials,Java Programming...