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

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.

Thanks 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.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileRead implements Runnable {
	
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {

		FileInputStream fis = null;
		
		//Open a file for read
		try {
			fis = new FileInputStream (new File ("C:\\temp\\ShareFile.txt"));

			//Read 500 bytes
			System.out.println ("Read thread prepares to read");
			int i = 0;
			for (i = 0; i < 500; i++)
			{
				System.out.print (fis.read());
			}

			System.out.println ("Read thread prepares to sleep");

			//Sleep for 5 seconds
			Thread.sleep (5000);

			System.out.println ("Read thread wakes up and prepares to read");

			//Read 500 bytes
			for (i = 0; i < 500; i++)
			{
				System.out.print (fis.read());
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			try {
				fis.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}

}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileWrite implements Runnable {

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		// TODO Auto-generated method stub
		RandomAccessFile raf = null;
		
		try {
			raf = new RandomAccessFile ("C:\\temp\\ShareFile.txt", "rw");
			
			System.out.println ("Write thread prepares to write");
			//write 1000 bytes
			byte[] buffer = new byte [1000];
			for (int i = 0; i < 1000; i ++)
			{
				buffer [i] = 0x32;
			}
			raf.seek (1000);
			Thread.sleep (2000);
			raf.write (buffer);
			
			System.out.println ("Write thread accomplishes writing");
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			try {
				raf.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

}
public class Driver {

	public static void main(String[] args) {

		new Thread (new FileRead()).start();
		new Thread (new FileWrite()).start();

	}

regards,
George

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


regards,
George

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.