1

Подскажите пожалуйста, как сделать, чтобы потоки синхронизировались по файлу "fileWithAnAdress.txt"

public class Main {

    public static void main(String[] args) {
        boolean delete1 = (new File("fileWithoutGarbage.txt")).delete();
        Thread thread = new Thread(new CreateFileService());
        thread.start();
        Thread thread1 = new Thread(new ReadFileService());
        thread1.start();
    }
}

public class CreateFileService implements Runnable {

    @Override
    public void run() {
       List<String> list = new ArrayList<>();
        list.add("'С:\\WebServers\\home\\testsite\\www\\myfile1.txt'");
        list.add("'С:\\WebServers\\home\\testsite\\www\\myfile2.txt'");
        list.add("'С:\\WebServers\\home\\testsite\\www\\myfile3.txt'");
        list.add("'С:\\WebServers\\home\\testsite\\www\\myfile4.txt'");
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("fileWithAnAdress.txt"));
            for (String s : list) {
                bw.write(s);
                bw.write(System.lineSeparator());
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class ReadFileService implements Runnable {

    @Override
    public void run() {
        List<String> lines = new ArrayList<>();
        List<String> newLines = new ArrayList<>();
        try {
            BufferedReader br = new BufferedReader(new FileReader("fileWithAnAdress.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
            System.out.println(lines);
            for (String s : lines) {
                String newString = s.substring(s.lastIndexOf('\\') + 1, s.lastIndexOf('.'));
                newLines.add(newString);
            }
            System.out.println(newLines);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("fileWithoutGarbage.txt"))) {
            for (String newLine : newLines) {
                bufferedWriter.write(newLine);
                bufferedWriter.write(System.lineSeparator());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        File file = new File("fileWithoutGarbage.txt");
        System.out.println(file.length() + " byte");
    }
}
2
  • Что вы хотите синхронизировать в потоках?
    – Roman C
    7 июл 2018 в 12:15
  • Чтобы второй поток запустился однозначно только после первого.Я пытаюсь разобраться, как это вообще происходит. Буду признателен за любую помощь.
    – avistate
    7 июл 2018 в 20:16

1 ответ 1

3

Непонятно зачем вам потоки, если вы запускаете их последовательно, но способов решить вашу задачу множество. Самый простой - дождаться завершения первого потока и потом запустить второй:

public static void main(String[] args) {
    boolean delete1 = (new File("fileWithoutGarbage.txt")).delete();
    Thread thread = new Thread(new CreateFileService());
    thread.start();

    thread.join();

    Thread thread1 = new Thread(new ReadFileService());
    thread1.start();
}

Обычно, когда порядок выполнения потоков роли не играет, можно использовать синхронизацию по общему ресурсу:

public class Main {
    public static void main(String[] args) {
        File file = new File("fileWithoutGarbage.txt");
        boolean delete1 = (file).delete();

        Thread thread = new Thread(new CreateFileService(file));
        thread.start();

        Thread thread1 = new Thread(new ReadFileService(file));
        thread1.start();
    }
}

public abstract class FileServices implements Runnable {
    protected File file;

    protected setFile(File file) {
        this.file = file;
    }
}

public class CreateFileService extends FileService {
    public CreateFileService(File file) {
        setFile(file);
    }

    @Override
    public void run() {
       synchronized(file) {
           ...
       }
    }
}

public class ReadFileService extends FileService {
    public ReadFileService(File file) {
        setFile(file);
    }

    @Override
    public void run() {
       synchronized(file) {
           ...
       }
    }
}

Но нет гарантии, что thread1 не начнёт выполняться раньше thread, поэтому в вашем случае этот способ не подходит. Придётся использовать какой-нибудь примитив синхронизации. Логика вашего кода - последовательность действий, разбитая на упорядоченные фазы - больше располагает к Phaser, но она настолько примитивна, что подойдёт и CountDownLatch:

public class Main {
    public static void main(String[] args) {
        boolean delete1 = (new File("fileWithoutGarbage.txt")).delete();
        CountDownLatch latch = new CountDownLatch(1);

        Thread thread = new Thread(new CreateFileService(latch));
        thread.start();

        Thread thread1 = new Thread(new ReadFileService(latch));
        thread1.start();
    }
}

public class CreateFileService implements Runnable {
    private final CountDownLatch latch;

    public CreateFileService(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        ...
        latch.countDown();
    }
}

public class ReadFileService implements Runnable {
    private final CountDownLatch latch;

    public ReadFileService(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        latch.await();
        ...
    }
}

Ну, или можно изобрести собственный ̶в̶е̶л̶о̶с̶и̶п̶е̶д̶ CountDownLatch на низкоуровневых конструкциях wait/notify, но если делать это правильно, то кода синхронизации получится больше, чем кода логики.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.