944,126 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 17130
  • Java RSS
Nov 24th, 2004
0

Question about file read/write

Expand Post »
Hello everyone,


If I am using two threads, the task of one thread is to read the first 1k bytes from a specific file and the task of second thread is to write (maybe modify without the change of 1k length, maybe append some additional bytes making the file larger, maybe remove some bytes making the file smaller) the last 1k bytes from the same specific file (the first 1k bytes and the last 1k bytes are not overlapped), need I add lock to the two threads so that they will work in a synchronized approach?

I think even though the two threads are operating the same file, but they are accessing (read/write) different parts, there is no need to add lock to the two threads to force them to work in a synchronized approach. Just let the two threads free to run in a simultaneous approach. Am I correct?


regards,
George
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Nov 24th, 2004
0

Re: Question about file read/write

only 1 thread can access the file at once as the file pointer WILL move during the read and write operations.

Say you're reading and have read 100 bytes. Now the other thread moves in and starts to write after setting the file position 1000 bytes from the end.

After 200 bytes the read thread begins again and tries to write 900 bytes.
Not only will it read from the wrong part of the file it will also try to read beyond end of file.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 26th, 2004
0

Re: Question about file read/write

Thanks jwenting,


Quote originally posted by jwenting ...
only 1 thread can access the file at once as the file pointer WILL move during the read and write operations.

Say you're reading and have read 100 bytes. Now the other thread moves in and starts to write after setting the file position 1000 bytes from the end.

After 200 bytes the read thread begins again and tries to write 900 bytes.
Not only will it read from the wrong part of the file it will also try to read beyond end of file.
I am using two different file pointers so that reading and writing operation is not targetted on the wrong part of the file.

I have accomplished a sample which shows that different threads accessing different parts of the same file does not need to be locked (synchronized). But I am not sure about whether my test case is correct to verify my idea. Can you help to review it and give your comments?

In my sample, ReadThread will read 500 bytes from the beginning of file (C:\\temp\\ShareFile.txt), then sleep for 5 seconds, and finally it will reads the continuous 500 bytes. WriteThread will write the last 1000 bytes from the same file.

Java Syntax (Toggle Plain Text)
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5.  
  6. public class FileRead implements Runnable {
  7.  
  8. /* (non-Javadoc)
  9. * @see java.lang.Runnable#run()
  10. */
  11. public void run() {
  12.  
  13. FileInputStream fis = null;
  14.  
  15. //Open a file for read
  16. try {
  17. fis = new FileInputStream (new File ("C:\\temp\\ShareFile.txt"));
  18.  
  19. //Read 500 bytes
  20. System.out.println ("Read thread prepares to read");
  21. int i = 0;
  22. for (i = 0; i < 500; i++)
  23. {
  24. System.out.print (fis.read());
  25. }
  26.  
  27. System.out.println ("Read thread prepares to sleep");
  28.  
  29. //Sleep for 5 seconds
  30. Thread.sleep (5000);
  31.  
  32. System.out.println ("Read thread wakes up and prepares to read");
  33.  
  34. //Read 500 bytes
  35. for (i = 0; i < 500; i++)
  36. {
  37. System.out.print (fis.read());
  38. }
  39. } catch (FileNotFoundException e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. } catch (IOException e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. } catch (InterruptedException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. } finally
  49. {
  50. try {
  51. fis.close();
  52. } catch (IOException e1) {
  53. // TODO Auto-generated catch block
  54. e1.printStackTrace();
  55. }
  56. }
  57.  
  58. }
  59.  
  60. }

Java Syntax (Toggle Plain Text)
  1. import java.io.FileNotFoundException;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;
  4.  
  5. public class FileWrite implements Runnable {
  6.  
  7. /* (non-Javadoc)
  8. * @see java.lang.Runnable#run()
  9. */
  10. public void run() {
  11. // TODO Auto-generated method stub
  12. RandomAccessFile raf = null;
  13.  
  14. try {
  15. raf = new RandomAccessFile ("C:\\temp\\ShareFile.txt", "rw");
  16.  
  17. System.out.println ("Write thread prepares to write");
  18. //write 1000 bytes
  19. byte[] buffer = new byte [1000];
  20. for (int i = 0; i < 1000; i ++)
  21. {
  22. buffer [i] = 0x32;
  23. }
  24. raf.seek (1000);
  25. Thread.sleep (2000);
  26. raf.write (buffer);
  27.  
  28. System.out.println ("Write thread accomplishes writing");
  29.  
  30. } catch (FileNotFoundException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. } catch (IOException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. } catch (InterruptedException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. } finally
  40. {
  41. try {
  42. raf.close();
  43. } catch (IOException e1) {
  44. // TODO Auto-generated catch block
  45. e1.printStackTrace();
  46. }
  47. }
  48. }
  49.  
  50. }

Java Syntax (Toggle Plain Text)
  1. public class Driver {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. new Thread (new FileRead()).start();
  6. new Thread (new FileWrite()).start();
  7.  
  8. }


regards,
George
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Dec 1st, 2004
0

Re: Question about file read/write

Thanks for all the people who have helped me on this thread.


regards,
George
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Text Searching
Next Thread in Java Forum Timeline: search engine speeds





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC