經過長期的研究後,我知道了這個字符串是不可改變.String緩衝區比字符串更爲有效,如果該程序涉及到很多計算。 但我的問題是從這些字符串VS StringBuffer的Java中
我有一個功能,我傳遞一個字符串略有不同。該字符串實際上是文章的文本(近3000-5000個字符)。該函數在線程中實現。我的意思是說,每次都有不同的字符串文本的多個函數調用。函數的後期計算過大。現在,當我爲大量線程運行我的代碼時,出現錯誤提示:超出了GC開銷限制。 。現在
,我不能減少功能的後一階段的計算,我的問題是,如果我改變從串來串緩衝區中的文字類型將它真的有用嗎?另外,我不對文本字符串進行任何連接操作。
我已經發布了我的代碼小snipet:
public static List<Thread> thread_starter(List<Thread> threads,String filename,ArrayList<String> prop,Logger L,Logger L1,int seq_no)
{ String text="";
if(prop.get(7).matches("txt"))
text=read_contents.read_from_txt(filename,L,L1);
else if(prop.get(7).matches("xml"))
text=read_contents.read_from_xml(filename,L,L1);
else if(prop.get(7).matches("html"))
text=read_contents.read_from_html(filename,L,L1);
else
{
System.out.println("not a valid config");
L1.info("Error : config file not properly defined for i/p file type");
}
/*TODO */
//System.out.println(text);
/*TODO CHANGES TO BE DONE HERE */
if(text.length()>0)
{
Runnable task = new MyRunnable(text,filename,prop,filename,L,L1,seq_no);
Thread worker = new Thread(task);
worker.start();
// Remember the thread for later usage
threads.add(worker);
}
else
{
main_entry_class.file_mover(filename, false);
}
return threads;
}
而且我多次撥打上面的函數使用下面的代碼:
List<Thread> threads = new ArrayList<Thread>();
thread_count=10;
int file_pointer=0;// INTEGER POINTER VARIABLE
do
{
if(file.size()<=file_pointer)
break;
else
{ String file_name=file.get(file_pointer);
threads=thread_starter(threads,file_name,prop,L,L1,seq_no);
file_pointer++;
seq_no++;
}
}while(check_status(threads,thread_count)==true);
而且檢查狀態功能:
public static boolean check_status(List<Thread> threads,int thread_count)
{
int running = 0;
boolean flag=false;
do {
running = 0;
for (Thread thread : threads) {
if (thread.isAlive()) {
//ThreadMXBean thMxB = ManagementFactory.getThreadMXBean();
//System.out.println(thMxB.getCurrentThreadCpuTime());
running++;
}
}
if(Thread.activeCount()-1<thread_count)
{
flag=true;
break;
}
} while (running > 0);
return flag;
}
你是什麼意思的「計算」? –
你基本上內存不足以運行流程smooth.Find是否有任何內存泄漏或增加jvm堆大小 – Kick
@David Wallace:通過計算,我的意思是執行名稱實體識別。名稱實體識別佔用大量內存。此外,因爲我不能減少NER部分中的任何內容,所以我只想知道從字符串更改爲字符串緩衝區是否有幫助。 – kiran