File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Safe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Safe" Watch "Thread Safe" New topic

Thread Safe

Shawn Kuenzler
Ranch Hand

Joined: Apr 16, 2006
Posts: 73
Are there any situations where you need to make your code thread safe if all it's doing is reading a file? This is kind of beyond the scope of the exam. Are there any file systems that won't allow concurrent read-only access to a file?

SCJP 1.5
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

I dont think multiple read operations might require thread safety, since you are not changing the contents of the file in any way.

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Dick Eimers
Ranch Hand

Joined: Apr 15, 2006
Posts: 40
Thread safety is only an issue when multiple threads concurrently read a single value AND at least one thread modifies this value at some point in time.

<a href="" target="_blank" rel="nofollow"></a>
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
Its not 100% that there is no synchronization problems while multiple threads reading from a single file.
ConsiderHere there is problem as a single thread can't read the file to the fullest.
So when each thread is supplying input to some other process then only a partial data of the file will be available from each thread.
This problem can't even be solved with the synchronized block, but it can be made sure that a single thread reads the whole file and other threads have no data to read at the end of first thread's execution.
To solve this problem you can have a FileReader object inside the run() method to ensure that each thread has the resource to read the same file.

But this example is an inefficient use of thread.
It is sorta covered in the JavaRanch Style Guide.
subject: Thread Safe
jQuery in Action, 3rd edition