Trts
Trts

Reputation: 1069

Lock a file and read it

This is a learning task. I want multiple threads to read files in a directory. If a thread finds an unoccupied file, it reads the file. Otherwise the thread will be terminated.

I'm trying to lock the file for reading and then read it symbol by symbol.

But in the readFile method I get IOException. How can I solve the problem?

This is my code:

private void burden() {

        File mainFolder = new File("C:\\FilesToRead");
        File[] files = mainFolder.listFiles();
        String freeFile;
        boolean jobExists = false;

        for (File file : files) {

            FileChannel channel;
            FileLock lock = null;
            try {
                channel = new RandomAccessFile(file, "rw").getChannel();;
                lock = channel.lock();
                // Ok. We get the lock
                String fl = file.getAbsolutePath();
                readFile(file);
                System.out.println(fl + " is captured.");
                System.out.println("Reading " + file);
            } catch (OverlappingFileLockException e) {
                continue; // File is open by someone else                 
            } catch (FileNotFoundException f) {

            } catch (IOException ex) {

            } catch (NonWritableChannelException n) {
                System.out.println("NonWritableChannelException");
            } finally {
                try {
                    lock.release();
                    System.out.println(file.getName() + " is released");
                } catch (IOException ex) {
                    System.out.println("IOException!");
                }
            }
        }
    } // burden();

    private void readFile(File file) {

        System.out.println("Reading " + file);

        FileReader inputStream = null;
        ArrayList<Integer> list = new ArrayList<>();
        try {
            int c;
            inputStream = new java.io.FileReader(file);

            while ((c = inputStream.read()) != -1) {
                if (Thread.currentThread().isInterrupted()) {
                    return;
                }
                list.add(c);
            }
            
        } catch (IOException e) {
            System.out.println("IOException!");
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ex) {
                    System.out.println("IOException!");
                }
            }
        }

    }

This is the picture (I translated the error message into English):

enter image description here

Added later: this is what I get in the debugger:

at java.io.FileInputStream.readBytes(Native Method)

at java.io.FileInputStream.read(FileInputStream.java:272)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:126)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
at java.io.InputStreamReader.read(InputStreamReader.java:168)
at parallelprogramming.Thrd.readFile(Thrd.java:82)
at parallelprogramming.Thrd.burden(Thrd.java:50)
at parallelprogramming.Thrd.run(Thrd.java:23)
at parallelprogramming.HotThrd.run(HotThrd.java:6)
at parallelprogramming.Thrd.<init>(Thrd.java:18)
at parallelprogramming.HotThrd.<init>(HotThrd.java:3)
at parallelprogramming.ThrdPool.addHotThrd(ThrdPool.java:40)
at parallelprogramming.ThrdPool.<init>(ThrdPool.java:29)
at parallelprogramming.ParallelProgramming.main(ParallelProgramming.java:18)

Upvotes: 0

Views: 145

Answers (1)

Boris the Spider
Boris the Spider

Reputation: 61198

From the Javadoc

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

Emphasis mine.

Upvotes: 1

Related Questions