Advertisement
Guest User

Part5

a guest
Jan 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package ua.nure.kudria.practice5;
  2.  
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. /**
  9.  * @author Artem Kudria
  10.  */
  11. public final class Part5 {
  12.     private static final String FILE_NAME = "part5.txt";
  13.     private static final List<Thread> threadList = new ArrayList<>();
  14.  
  15.     private Part5() {
  16.     }
  17.  
  18.     public static void main(String[] args) {
  19.         try {
  20.             final RandomAccessFile accessFile = new RandomAccessFile(FILE_NAME, "rw");
  21.             for (int i = 0; i < 10; i++) {
  22.                 synchronized (accessFile) {
  23.                     Thread thread = new Thread(new RandomThread(accessFile, i));
  24.                     threadList.add(thread);
  25.                     thread.start();
  26.                 }
  27.             }
  28.             synchronized (threadList) {
  29. //                accessFile.close();
  30.             }
  31.         } catch (IOException e) {
  32.             System.err.println(e.getMessage());
  33.         }
  34.  
  35.         new Thread(() -> {
  36.             synchronized (threadList) {
  37.                 try {
  38.                     System.out.println(read());
  39.                 } catch (IOException e) {
  40.                     System.err.println(e.getMessage());
  41.                 }
  42.             }
  43.         }).start();
  44.     }
  45.  
  46.     public static String read() throws IOException {
  47.         RandomAccessFile file;
  48.         file = new RandomAccessFile(FILE_NAME, "r");
  49.         String res = "";
  50.         int b = file.read();
  51.         while (b != -1) {
  52.             res = res + (char) b;
  53.             b = file.read();
  54.         }
  55.         file.close();
  56.  
  57.         return res;
  58.     }
  59. }
  60.  
  61. class RandomThread implements Runnable {
  62.     private int number;
  63.     private final RandomAccessFile randomAccessFile;
  64.  
  65.     public RandomThread(RandomAccessFile randomAccessFile, int number) {
  66.         this.number = number;
  67.         this.randomAccessFile = randomAccessFile;
  68.     }
  69.  
  70.     @Override
  71.     public void run() {
  72.         synchronized (randomAccessFile) {
  73.             try {
  74.                 randomAccessFile.seek(randomAccessFile.length());
  75.                 for (int i = 0; i < 20; i++) {
  76.                     randomAccessFile.writeBytes(Integer.toString(number));
  77.                     Thread.sleep(1);
  78.                 }
  79.                 randomAccessFile.write(System.lineSeparator().getBytes());
  80.             } catch (IOException | InterruptedException e) {
  81.                 System.err.println(e.getMessage());
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement