1 package com.calipso.reportgenerator.reportcalculator; 2 3 import com.calipso.reportgenerator.common.LanguageTraslator; 4 5 import java.io.*; 6 import java.nio.channels.FileChannel ; 7 import java.nio.channels.FileLock ; 8 import java.sql.ResultSet ; 9 10 16 public class BlockSerializerThread extends Thread { 17 18 private String blockFileName; 19 private boolean serialized; 20 private Matrix matrix; 21 private Exception savedException; 22 23 public BlockSerializerThread(String blockFileName){ 24 super(); 25 this.blockFileName = blockFileName; 26 } 27 28 public BlockSerializerThread(String fileName, Matrix usedPart) { 29 super(); 30 this.blockFileName = fileName; 31 this.matrix = usedPart; 32 } 33 34 public void run() { 35 try{ 36 if(matrix==null){ 37 loadBlock(); 39 }else{ 40 serializeBlock(); 42 } 43 }catch (Exception e){ 44 e.printStackTrace(); 46 savedException = e; 47 }catch (Throwable t){ 48 savedException = new Exception (t); 49 } 50 System.out.println(LanguageTraslator.traslate("487")); 51 System.out.println(LanguageTraslator.traslate("488") + blockFileName); 52 System.gc(); 53 System.gc(); 54 System.gc(); 55 System.gc(); 56 System.gc(); 57 System.out.println("5 gc()" + blockFileName); 58 } 59 60 private void serializeBlock() throws Exception , Throwable { 61 System.out.println(LanguageTraslator.traslate("489") + blockFileName); 62 File file = new File(blockFileName); 63 if(file.exists()){ 64 file.delete(); 65 } 66 FileOutputStream fileOutputStream = new FileOutputStream(blockFileName); 67 FileChannel channel = fileOutputStream.getChannel(); 68 FileLock lock = channel.tryLock(); 69 ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); 70 objectOutputStream.writeObject(matrix); 71 matrix=null; 72 lock.release(); 73 objectOutputStream.flush(); 74 fileOutputStream.flush(); 75 objectOutputStream.close(); 76 fileOutputStream.close(); 77 } 78 79 private synchronized void loadBlock() throws Exception { 80 System.out.println(LanguageTraslator.traslate("490") + blockFileName); 81 FileInputStream fileInputStream = new FileInputStream(blockFileName); 82 while(matrix==null){ 84 try{ 85 ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); 86 matrix = (Matrix)objectInputStream.readObject(); 87 objectInputStream.close(); 88 }catch(IOException e){ 89 sleep(10000); 91 } 92 } 93 fileInputStream.close(); 94 notify(); 95 } 96 97 public synchronized Matrix getPreparedPart() throws Exception { 98 while(matrix==null && savedException==null){ 99 wait(); 100 } 101 if(savedException!=null){ 102 System.out.println(LanguageTraslator.traslate("491")); 103 savedException.printStackTrace(); 104 throw savedException; 105 } 106 return matrix; 107 } 108 109 } 110 111 | Popular Tags |