import java.io.*; import java.util.*; import java.util.concurrent.locks.*; public class Sangtekst { private Lock l?s = new ReentrantLock(); private HashSet ord = new HashSet<>(); public HashSet hentOrd() { l?s.lock(); try { return ord; } finally { l?s.unlock(); } } public void leggTilOrd(HashSet lokalSet) { for (String s : lokalSet) { l?s.lock(); try { ord.add(s); } finally { l?s.unlock(); } } } public void lesSangTekst(String filnavn) { try { Scanner sc = new Scanner(new File(filnavn)); HashSet lokalSet = new HashSet<>(); while (sc.hasNextLine()) { String line = sc.nextLine(); System.out.println("Read " + line + " in " + filnavn); String[] lineArray = line.split(" "); for (String s : lineArray) { lokalSet.add(s); } } System.out.println(filnavn + " er ferdig lest!"); sc.close(); leggTilOrd(lokalSet); } catch (FileNotFoundException e) { System.out.println(filnavn + " ikke funnet"); System.exit(1); } } }