package ejercicio.ejer_t5_6_4;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class FindDuplicatesConcurrent {

	private static final int N_THREADS = 4;
	private ConcurrentMap<String, String> duplicates = new ConcurrentHashMap<>();
	private Object screenLock = new Object();

	private ExecutorService exec = Executors.newFixedThreadPool(N_THREADS);
	
	private AtomicInteger tareasPendientes = new AtomicInteger();	
	
	public void find(File dir) {
		findConcurrent(dir);
		
		//Esperar a que todas las tareas terminen
		try {
			exec.awaitTermination(2000000, TimeUnit.DAYS);
		} catch (InterruptedException e) {
		}
	}
	
	public void findConcurrent(File dir) {		

		for (File file : dir.listFiles()) {

			if (file.isDirectory()) {
				
				exec.execute(()-> findConcurrent(file));
				tareasPendientes.incrementAndGet();

			} else {

				String path = duplicates.putIfAbsent(file.getName(),
						file.getAbsolutePath());

				if (path != null) {
					synchronized (screenLock) {
						System.out.println(
								"Found duplicate file: " + file.getName());
						System.out.println("    " + path);
						System.out.println("    " + file.getAbsolutePath());
					}
				}
			}
		}
		
		if(tareasPendientes.decrementAndGet() == 0){
			exec.shutdown();
		}
	}

	public static void main(String[] args) {

		new FindDuplicatesConcurrent().find(new File(
				"/home/mica/Data/Docencia/Asignaturas/PC/2015-2016"));
		
		System.out.println("Terminado");

	}
}

@@ -53,61 +53,8 @@
 e;%0D%0A
-import java.util.ArrayList;%0D%0Aimport java.util.List;%0D%0A
 impo
@@ -144,184 +144,8 @@
 ap;%0D
-%0Aimport java.util.concurrent.ExecutorService;%0D%0Aimport java.util.concurrent.Executors;%0D%0Aimport java.util.concurrent.TimeUnit;%0D%0Aimport java.util.concurrent.atomic.AtomicInteger;%0D
 %0A%0D%0Ap
@@ -190,50 +190,8 @@
 %0D%0A%0D%0A
-%09private static final int N_THREADS = 4;%0D%0A
 %09pri
@@ -270,197 +270,8 @@
 );%0D%0A
-%09private Object screenLock = new Object();%0D%0A%0D%0A%09private ExecutorService exec = Executors.newFixedThreadPool(N_THREADS);%0D%0A%09%0D%0A%09private AtomicInteger tareasPendientes = new AtomicInteger();%09%0D%0A%09
 %0D%0A%09p
@@ -289,250 +289,63 @@
 find
+Duplicates
 (File 
-dir
+root
 ) %7B%0D%0A%09%09
-findConcurrent(dir);%0D%0A%09%09%0D%0A%09%09//Esperar a que todas las tareas terminen%0D%0A%09%09try %7B%0D%0A%09%09%09exec.awaitTermination(2000000, TimeUnit.DAYS);%0D%0A%09%09%7D catch (InterruptedException e) %7B%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%09public void findConcurrent(File dir) %7B%09%09%0D%0A%0D%0A
+if (root.isDirectory()) %7B%0D%0A%09
 %09%09fo
@@ -363,11 +363,12 @@
 e : 
-dir
+root
 .lis
@@ -380,18 +380,17 @@
 s()) %7B%0D%0A
-%0D%0A
+%09
 %09%09%09if (f
@@ -419,99 +419,33 @@
 %09%09%09%09
-%0D%0A%09%09%09%09exec.execute(()-%3E findConcurrent
+%09findDuplicates
 (file)
-)
 ;%0D%0A%09
-%09%09%09tareasPendientes.incrementAndGet();%0D%0A%0D%0A
 %09%09%09%7D
@@ -451,24 +451,25 @@
 %7D else %7B%0D%0A%0D%0A
+%09
 %09%09%09%09String p
@@ -512,24 +512,17 @@
 tName(),
-%0D%0A%09%09%09%09%09%09
+ 
 file.get
@@ -541,24 +541,25 @@
 ());%0D%0A%0D%0A%09%09%09%09
+%09
 if (path != 
@@ -571,42 +571,8 @@
  %7B%0D%0A
-%09%09%09%09%09synchronized (screenLock) %7B%0D%0A
 %09%09%09%09
@@ -596,18 +596,8 @@
 tln(
-%0D%0A%09%09%09%09%09%09%09%09
 %22Fou
@@ -659,26 +659,24 @@
 ut.println(%22
-  
   %22 + path);
@@ -703,18 +703,16 @@
 rintln(%22
-  
   %22 + fi
@@ -766,285 +766,167 @@
 %7D%0D%0A%09
-%09%0D%0A%09%09if(tareasPendientes.decrementAndGet() == 0)%7B%0D%0A%09%09%09exec.shutdown();%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09public static void main(String%5B%5D args) %7B%0D%0A%0D%0A%09%09new FindDuplicatesConcurrent().find(new File(%0D%0A%09%09%09%09%22/home/mica/Data/Docencia/Asignaturas/PC/2015-2016%22));%0D%0A%09%09%0D%0A%09%09System.out.println(%22Terminado%22);%0D%0A
+%7D%0D%0A%0D%0A%09public void exec() %7B%0D%0A%09%09new Thread(() -%3E findDuplicates(new File(%22X:%5C%5CDir%22))).start();%0D%0A%09%09new Thread(() -%3E findDuplicates(new File(%22X:%5C%5CDir2%22))).start();
 %0D%0A%09%7D

Solve 5.6.4 ejer without executors