Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.util.*;
- class lab3 extends Thread {
- public boolean isTrue (StringBuilder[] str){
- for (StringBuilder stringBuilder : str) {
- if (stringBuilder.length() < 0)
- return false;
- }
- return true;
- }
- @Override
- public void run() {
- FileWriter fw = null;
- try {
- fw = new FileWriter("result.txt");
- } catch (IOException e) {
- e.printStackTrace();
- }
- assert fw != null;
- TreeSet<String> set = new TreeSet<>();
- BufferedWriter bufferWriter = new BufferedWriter(fw);
- WriteFile[] obj_arr = new WriteFile[10];
- StringBuffer[] str = new StringBuffer[10];
- for(int i = 0; i<10; i++){
- str[i] = new StringBuffer();
- obj_arr[i] = new WriteFile(bufferWriter, str[i]);
- obj_arr[i].start();
- }
- int before = 3, after = 3;
- String searchLine = "123";
- ArrayList<String> lines = new ArrayList<>();
- Scanner filein = null;
- try {
- filein = new Scanner(new File("file.txt"));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- assert filein != null;
- FileReader input = null;
- try {
- input = new FileReader("file.txt");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- assert input != null;
- LineNumberReader count = new LineNumberReader(input);
- long count_lines = 0;
- while (true)
- {
- try {
- if (!(count.skip(Long.MAX_VALUE) > 0)) break;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- count_lines = count.getLineNumber() + 1;
- int output_strings =0, current_count_lines = 0, currentThreadIndex = 0;
- long start = System.currentTimeMillis();
- ArrayDeque<Integer> Arr = new ArrayDeque<>();
- while (filein.hasNextLine()){
- current_count_lines++;
- lines.add(filein.nextLine());
- if(lines.size() > before + after + 1){
- lines.remove(0);
- }
- if (lines.size() >= after + 1){
- if(lines.get(lines.size() - after - 1).contains(searchLine)){
- Arr.add(output_strings);
- StringBuilder tmp = new StringBuilder();
- for (String line:
- lines) {
- output_strings++;
- tmp.append(output_strings);
- tmp.append(" ");
- tmp.append(line);
- tmp.append("\n");
- }
- while (str[0].length() > 0 && str[1].length() > 0 && str[2].length() > 0 && str[3].length() > 0 &&
- str[4].length() > 0 && str[5].length() > 0 && str[6].length() > 0 && str[7].length() > 0 &&
- str[8].length() > 0 && str[9].length() > 0){
- try {
- sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if(str[0].length() == 0) {
- str[0].append(tmp);
- // Arr.add(output_strings);
- }else if(str[1].length() == 0) {
- str[1].append(tmp);
- Arr.add(output_strings);
- }else if(str[2].length() == 0) {
- str[2].append(tmp);
- }else if(str[3].length() == 0) {
- str[3].append(tmp);
- }else if(str[4].length() == 0) {
- str[4].append(tmp);
- }else if(str[5].length() == 0) {
- str[5].append(tmp);
- }else if(str[6].length() == 0) {
- str[6].append(tmp);
- }else if(str[7].length() == 0) {
- str[7].append(tmp);
- }else if(str[8].length() == 0) {
- str[8].append(tmp);
- }else{
- str[9].append(tmp);
- }
- // lines.remove(0);
- }
- }
- long time = System.currentTimeMillis() - start;
- long cur = (count_lines - current_count_lines)*(time/1000)/current_count_lines;
- System.out.println("Найдено совпадений "+output_strings);
- System.out.println("Осталось работать "+cur+" сек");
- }
- lines.remove(0);
- for(int i = before + 1; i < lines.size(); i++) {
- if (lines.get(i).contains(searchLine)) {
- Arr.add(output_strings);
- StringBuilder tmp = new StringBuilder();
- for (String line :
- lines) {
- output_strings++;
- tmp.append(output_strings);
- tmp.append(" ");
- tmp.append(line);
- tmp.append("\n");
- }
- while (str[0].length() > 0 && str[1].length() > 0 && str[2].length() > 0 && str[3].length() > 0 &&
- str[4].length() > 0 && str[5].length() > 0 && str[6].length() > 0 && str[7].length() > 0 &&
- str[8].length() > 0 && str[9].length() > 0) {
- try {
- sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (str[0].length() == 0) {
- str[0].append(tmp);
- // Arr.add(output_strings);
- } else if (str[1].length() == 0) {
- str[1].append(tmp);
- Arr.add(output_strings);
- } else if (str[2].length() == 0) {
- str[2].append(tmp);
- } else if (str[3].length() == 0) {
- str[3].append(tmp);
- } else if (str[4].length() == 0) {
- str[4].append(tmp);
- } else if (str[5].length() == 0) {
- str[5].append(tmp);
- } else if (str[6].length() == 0) {
- str[6].append(tmp);
- } else if (str[7].length() == 0) {
- str[7].append(tmp);
- } else if (str[8].length() == 0) {
- str[8].append(tmp);
- } else {
- str[9].append(tmp);
- }
- lines.remove(0);
- }
- }
- while (str[0].length() > 0 || str[1].length() > 0 || str[2].length() > 0 ||str[3].length() > 0 ||
- str[4].length() > 0 || str[5].length() > 0 || str[6].length() > 0 || str[7].length() > 0 ||
- str[8].length() > 0 || str[9].length() > 0) {
- try {
- sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- try {
- bufferWriter.close();
- fw.close();
- filein.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- for(int i = 0; i<10; i++){
- obj_arr[i].interrupt();
- }
- // ArrayList<StringBuffer> str2;
- this.interrupt();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement