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
.
and..
get stuck? - Saidolim