There is a code that converts files from fb2 files to txt. I run the process in 4 threads. At first, everything pretty quickly counts (10 files for 5 seconds), and then the speed gets smaller and smaller. What could be the problem?

public class Converter { TaskManager taskManager = new TaskManager(4); public void start(File f, String path){ taskManager.start(); dirToTxt(f, path); } private void dirToTxt(File f, String dir2) { if (f.isDirectory()) { File[] files = f.listFiles(); for (File file : files) { dirToTxt(file,dir2); } } else { Task task = new Task(){ @Override public void run() { try { DocumentParser documentParser = new DocumentParser(f); Document document = documentParser.parseDocument(); DefaultFb2 fb2 = new DefaultFb2(document); fb2.saveAsTxt(dir2); } catch (Exception e) { System.out.println(e); } } }; taskManager.addTask(task); } } } public class TaskManager { int threadscount; Thread[] threads; TaskBuffer taskBuffer = new TaskBuffer(); boolean isWorking = false; public TaskManager(int threadscount){ this.threadscount = threadscount; threads = new Thread[threadscount]; for(int i = 0;i<threadscount;i++){ threads[i] = new Thread(new Thread(){ @Override public void run() { while (isWorking){ taskBuffer.getTask().run(); if(taskBuffer.getSize()<1)isWorking=false; try { Thread.sleep(100); }catch (Exception e){System.out.println(e);} } } }); } } private void _run(){ for (int i = 0;i<threadscount;i++){ threads[i].start(); } } public void start(){ Thread chkThread = new Thread(new Thread(){ @Override public void run() { while (!isWorking){ try { if (taskBuffer.getSize() > 0) { isWorking = true; _run(); } Thread.sleep(500); }catch (Exception e){System.out.println(e);} } } }); chkThread.start(); } public void addTask(Task t){ taskBuffer.addTask(t); } } 

PS Also in peak can occupy memory to 1 gigabyte. Although it is absolutely incomprehensible where ... Also, the amount of memory does not change much when starting 1 or 20 threads

  • can folders . and .. get stuck? - Saidolim
  • @Saidolim is not, specially put everything in one daddy so that you can adequately assess the speed - max619
  • IMHO, garbage collector. - user194374

1 answer 1

To understand what memory is spent on, you need to perform "profiling"!

And do not pile constructors, it is better to create an init method and call it at the beginning of the start method, although in your case this is not critical, but still, in most cases, this is bad practice and you should not get used to it.

P. S. I didn’t really get into your code, but it seems to me that too many threads are being created. I am confused by a call from a loop in a separate thread, a method that uses a loop to start threads.