Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void run() {
- try {
- while (true) {
- if (this.partition.getWriteDone() && this.partition.count() == 0) {
- System.out.println("partition[" + this.index + "] write done");
- break;
- }
- String word = this.partition.pop();
- String lowerCaseWord = word.toLowerCase();
- if (trie.search(lowerCaseWord)) {
- System.out.println("ignore redundant word: " + word);
- continue;
- } else {
- trie.insert(lowerCaseWord);
- }
- String filePath = this.makePath(word);
- String finalWord = word + "rn";
- byte[] content = finalWord.getBytes();
- FileOutputStream out = null;
- // Case 1. file lock not used
- try {
- out = new FileOutputStream(filePath, true);
- out.write(content);
- out.flush();
- out.close();
- } catch (Exception e) {
- System.out.println("Consumer[" + this.index + "] Exception error: " + e);
- }
- // Case 2. file lock used
- /*try {
- out = new FileOutputStream(filePath, true);
- try {
- FileLock lock = out.getChannel().lock();
- try {
- out.write(content);
- } catch(Exception e) {
- } finally {
- if (lock != null) {
- lock.release();
- }
- }
- } catch(OverlappingFileLockException e) {
- trie.delete(lowerCaseWord, 0, trie.root);
- this.partition.push(word);
- } finally {
- out.close();
- }
- } catch(Exception e) {
- }*/
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement