Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. @Override
  2. public void run() {
  3. try {
  4. while (true) {
  5. if (this.partition.getWriteDone() && this.partition.count() == 0) {
  6. System.out.println("partition[" + this.index + "] write done");
  7. break;
  8. }
  9.  
  10. String word = this.partition.pop();
  11. String lowerCaseWord = word.toLowerCase();
  12.  
  13. if (trie.search(lowerCaseWord)) {
  14. System.out.println("ignore redundant word: " + word);
  15. continue;
  16. } else {
  17. trie.insert(lowerCaseWord);
  18. }
  19.  
  20. String filePath = this.makePath(word);
  21. String finalWord = word + "rn";
  22. byte[] content = finalWord.getBytes();
  23. FileOutputStream out = null;
  24.  
  25. // Case 1. file lock not used
  26. try {
  27. out = new FileOutputStream(filePath, true);
  28. out.write(content);
  29. out.flush();
  30. out.close();
  31. } catch (Exception e) {
  32. System.out.println("Consumer[" + this.index + "] Exception error: " + e);
  33. }
  34. // Case 2. file lock used
  35. /*try {
  36. out = new FileOutputStream(filePath, true);
  37. try {
  38. FileLock lock = out.getChannel().lock();
  39. try {
  40. out.write(content);
  41. } catch(Exception e) {
  42.  
  43. } finally {
  44. if (lock != null) {
  45. lock.release();
  46. }
  47. }
  48. } catch(OverlappingFileLockException e) {
  49. trie.delete(lowerCaseWord, 0, trie.root);
  50. this.partition.push(word);
  51. } finally {
  52. out.close();
  53. }
  54. } catch(Exception e) {
  55.  
  56. }*/
  57. }
  58. } catch (InterruptedException e) {
  59. // TODO Auto-generated catch block e.printStackTrace();
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement