0

The first thread determines the number of spaces in the file, if the number of spaces is even - the second stream capitalizes the first letters of all words in the file, if odd - the last letters. With the wait and notify.Something is wrong with wait and notify, creates a short circuit and it does not work.

    public class Dispatcher {

    public static void main(String[] args) {
        FileHandler fileHandler = new FileHandler();
        Thread thread = new CountSpaces(new File("text.txt"), fileHandler);
        Thread thread1 = new ChangeText(new File("text.txt"), fileHandler);

        thread.start();
        thread1.start();
    }

}

class CountSpaces extends Thread{
    File file;
    FileHandler fileHandler;

    public CountSpaces(File file, FileHandler fileHandler) {
        this.file = file;
        this.fileHandler = fileHandler;
    }


    @Override
    public void run() {
        fileHandler.countSpace(file);
    }
}

class ChangeText extends Thread {
    File file;
    FileHandler fileHandler;

    public ChangeText(File file, FileHandler fileHandler) {
        this.file = file;
        this.fileHandler = fileHandler;
    }

    @Override
    public void run() {
        try {
            fileHandler.changeLetters(file);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

class FileHandler {
    boolean flag = false;
    int count;
    void countSpace(File file) {
        try (Scanner sc = new Scanner(file)) {
            synchronized (this){
                wait();
                while (sc.hasNext() && !flag) {
                    count++;
                    sc.next();
                }
                flag = true;
                notify();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void changeLetters(File file) throws InterruptedException {
        try (Scanner sc = new Scanner(file);
             PrintWriter printWriter = new PrintWriter(file + "result")) {
                synchronized (this){
                    wait();
                    if (count % 2 == 0) {
                        while (sc.hasNext() && flag) {
                            String word = sc.next();
                            printWriter.print(word.substring(0, 1).toUpperCase() + word.substring(1));
                        }
                    } else  {
                        while (sc.hasNext() && flag) {
                            String word = sc.next();
                            printWriter.print(word.substring(1, word.length() - 1)
                                    + word.substring(word.length() - 1).toUpperCase());
                        }
                    }
                    flag = false;
                    notify();
                }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • The only way to do this is to count all the spaces in the file first, yes? So you have to wait for the first task to complete. That's it, just wait until it is 100% done. – markspace Dec 06 '21 at 16:00
  • 1
    Your description says that one thread is supposed to perform a different action than the other, but both threads are executing the same code, just for different files. Further you’re reading and writing the shared variable `flag` outside of `synchronized` blocks, so it’s not guaranteed that the threads see each others updates, not that it matters much here. Both methods are repeatedly calling `wait()` in a loop, but have only a single `notify()` at the end, which they never reach anyway, so it’s not even a problem that this `notify()` outside `synchronized` will throw an exception. – Holger Dec 06 '21 at 16:34
  • If to do through two threads it should be for example a class ChangeLetters and CountSpaces, and at each method run () where there should be a boolean flag? –  Dec 06 '21 at 17:17
  • Always call wait in a loop, see oracle's guarded blocks page in the concurrency tutorial. The changeLetters method isn't doing this. Also be more specific than "Does not work". – Nathan Hughes Dec 08 '21 at 14:13
  • Related: https://stackoverflow.com/q/1038007/217324 – Nathan Hughes Dec 08 '21 at 14:24

0 Answers0