IntelliJ Java IDE
The moose likes Threads and Synchronization and the fly likes How to synchronize? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "How to synchronize?" Watch "How to synchronize?" New topic
Author

How to synchronize?

Renata fonseca
Ranch Hand

Joined: Mar 15, 2002
Posts: 48
I have a method that access some DATABASE and write the result to a file. Like this...

yyy class will be INSTANTIATE by the servlet.
The point is IF some request execute the first line (#1), it MUST execute the second line (#2) IMMEDIATELY because I want to preserve the order of line that is writing to file. If some other threading (by another client request) execute between #1 and #2 of firt request, I'll have a file output like this:
-- somefile.txt (just an example) --
BD - Autoincrement column = 2
BD - Autoincrement column = 1
I tried to put synchronized keyword in the method, but it didn't work.
My test: I press the F5 key (refresh) of browser many times (quickly)... and when I see the file, some line is not in order.
Why didn't synchronized help me?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
My guess - x() is a non-static method, so when you make it synchronized it will acquire a lock on the instance of the class it's in. However if different threads have different instances of this class, they are acquiring different locks, and synchronization does nothing to prevent the methods from running concurrently. Since there is only one file system which all threads are accessing, you probably want a single lock which all threads must use to access the file system. This means you need to synchronize on some sort of static object (so that all threads are synchronizing on the same object). A common choice here would be to use the Class object associated with the class you're writing. E.g.:

"yyy.class" is a Class literal representing the Class object associated with the class yyy. It's the same object that would've been used as a monitor if method x() had been declared as static synchronized. (This would also be a way to force all threads to use the same lock; I'm assuming that x() is nonstatic for a reason though.)
However, this also causes me to wonder if there's even any point to using multiple threads in this program. If the threads are also doing other things, and this business of querying the database and updating files is only a part of their job description, then OK, threads may be useful here. But if the threads don't have anything useful to do other than wait for other threads to finish using the file system, you might as well just use one thread to do that. Depends on your application I suppose...
[ January 31, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1158

create a new class that does the file writing. within that class you can synchronize and only allow 1 thread to write at a time.
You need to be writing from 1 place only before you are able to control the writing in that manner.
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Would using a 'singleton' pattern for getting a class be beneficial here?
Paul
 
IntelliJ Java IDE
 
subject: How to synchronize?
 
Threads others viewed
How to synchronize
Lock question!
threads and synchronization
nx:All of URLy Bird 1.1.3 about read/write lock
Trouble with BigDecimal and Double