[英]How does a program run after main() exits?
我編寫了一個程序,該程序使用twitter流通過BufferedWriter
將tweet實時寫入File
。 但是bufferedWriter直到我在main函數末尾調用close()
方法時才寫入文本。
現在,當我運行程序時,首先關閉文件,然后開始發送tweet。 主出口后這東西怎么工作???
這是代碼:
package analytics;
import twitter4j.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
公共最終課程Trial_Filters {
static String Tweet;
static FileWriter output;
static BufferedWriter writer;
public static void main(String[] args) throws TwitterException,IOException {
if (args.length < 1) {
System.out.println("Usage: java twitter4j.examples.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]");
System.exit(-1);
}
output= new FileWriter("Log.txt");
writer=new BufferedWriter(output);
StatusListener listener = new StatusListener() {
public void onStatus(Status status) {
StringBuilder temp;
//System.out.print("<sta>"); // Start Status -- helps for parsing two lines tweet;<sta> and </sta> used as tweet delimiters
temp=new StringBuilder("<sta>");
if(status.isRetweet())
temp.append("%");//System.out.print("%"); // easier to identify ReTweets
//System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
temp.append("@" + status.getUser().getScreenName() + " - " + status.getText());
//System.out.print("</sta>"); //End Status
temp.append("</sta>");
Tweet=temp.toString();
this.add_to_Log();
}
private void add_to_Log(){
// TODO Auto-generated method stub
try{
output= new FileWriter("Log.txt");
writer=new BufferedWriter(output);
writer.write(Tweet);
System.out.println(Tweet);
}
catch(Exception e)
{
System.out.println(e);
}
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
public void onException(Exception ex) {
ex.printStackTrace();
}
};
TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
twitterStream.addListener(listener);
ArrayList<Long> follow = new ArrayList<Long>();
ArrayList<String> track = new ArrayList<String>();
for (String arg : args) {
if (isNumericalArgument(arg)) {
for (String id : arg.split(",")) {
follow.add(Long.parseLong(id));
}
} else {
track.addAll(Arrays.asList(arg.split(",")));
}
}
long[] followArray = new long[follow.size()];
for (int i = 0; i < follow.size(); i++) {
followArray[i] = follow.get(i);
}
String[] trackArray = track.toArray(new String[track.size()]);
// filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
twitterStream.filter(new FilterQuery(0, followArray, trackArray));
try{
System.out.println("bye");
writer.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
private static boolean isNumericalArgument(String argument) {
String args[] = argument.split(",");
boolean isNumericalArgument = true;
for (String arg : args) {
try {
Integer.parseInt(arg);
} catch (NumberFormatException nfe) {
isNumericalArgument = false;
break;
}
}
return isNumericalArgument;
}
}
虛擬機終止所有活動,並在最后一個非守護程序線程(“用戶線程”)終止 (或某些調用System.exit()
線程)之后退出。 最后一個用戶線程不必是主線程。
這些推文 (數據包)被發送到本地套接字,並且只有在虛擬機啟動並運行的情況下,套接字才會被綁定(以防未手動關閉套接字的情況)。 因此,提要可能正在發送,但計算機將不接受數據,並且鳴叫源將收到錯誤。
TwitterStream對象將啟動一個線程,該線程將在StatusListener上回調。 程序將一直運行,直到有一個線程在運行,即使主線程似乎已經完成,它也會在主線程停止之前等待所有其他線程停止,然后程序退出。
是否在main的末尾關閉寫入器並不重要。 在對add_to_log的每次調用中都會重寫作者的引用。 這意味着在每個新狀態下都會實例化一個新的編寫器,並將消息寫出到緩沖的編寫器中。
代碼開頭的以下兩行無關緊要:
output= new FileWriter("Log.txt");
writer=new BufferedWriter(output);
但是,最好在add_to_log中調用flush()或close()以確保所有內容都已寫入磁盤。
當您在激活了bufferedWriter的情況下關閉程序時,系統觸發緩沖的寫入器將其輸出刷新到它所連接的任何端點,無論是文件還是stdout。 隨着時間的流逝,可能會發生的情況是,文檔正到達套接字上並被放入bufferedwriter中,但是您的writer只是將它們存儲起來,因為您沒有從代碼或編寫器閾值中調用flush()來執行轉儲尚未完成。 當您在內部調用close時,即使程序是“ closed”,它也會調用flush並強制輸出轉儲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.