栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

线程不会在run()结束时自然退出

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线程不会在run()结束时自然退出

因此,事实证明这是一个解决方案,我对昨天有个想法,但从未采取任何行动。

从本质上讲,我遇到的问题的根源(我认为)是由于写缓冲区溢出导致的,导致未定义的行为。

现在,我不知道这是由于Java bufferedwriter的错误Java实现还是由于到底发生了什么,但是解决方案却变得相对简单:现在
每进行一次迭代就
刷新流,我知道您知道什么在想,加!经常这样!减速一定是巨大的!是的,确实如此,增速放缓是巨大的,它使解析文件的时间从大约14秒缩短到大约4分钟达到了1700万行。

可能我可以稍微提高刷新次数以提高性能,但是每10次刷新一次仍然失败。

我敢肯定,这是Java中如何处理读/写操作和内存管理的内部结果,而且我没有时间去研究它。如果有人想花时间对这种行为做出很好的解释,我会很乐意将我接受的答案切换到他们的帖子,因为它更加完整。

固定类(现在可以使用)的代码以

DateWriter
供将来检查:

package File_Conversion;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.text.DecimalFormat;import java.util.ArrayList;import javafx.application.Platform;import javafx.scene.control.ProgressIndicator;public class DateWriter implements Runnable {    private final ProgressIndicator myIndicator;    private static File ncFile;    private final String outputLocationFile;    private float zmax, zmin, xmax, xmin, ymax, ymin;    private ArrayList<Float> xList, yList, zList;    private final DecimalFormat numberFormat = new DecimalFormat("#.000000");    private final DecimalFormat numberFormatMinMax = new DecimalFormat("#.00000");    private final boolean yr_mon_day;        public DateWriter(String inputNCFile, String outputLocation, ProgressIndicator myIndicator, boolean yr_mon_day) {        this.yr_mon_day = yr_mon_day;        this.myIndicator = myIndicator;        ncFile = new File(inputNCFile);        outputLocationFile = outputLocation;    }        @Override    public void run() {        convertDate();        Platform.runLater(new Runnable() { @Override public void run() {     File_Conversion.stage_returner().close(); }        });        System.out.println("At the end of the method.");    }    public boolean convertDate() {        BufferedReader br = null;        java.io.FileWriter yearWriter = null, monthWriter = null, dayWriter = null, fWriter = null;        BufferedWriter yearBuf = null, monthBuf = null, dayBuf = null, writer = null;        try { br = new BufferedReader(new FileReader(ncFile)); if (yr_mon_day) {     yearWriter = new java.io.FileWriter(outputLocationFile + "\" + ncFile.getName().substring(0, ncFile.getName().lastIndexOf(".")) + "_modified_year.csv", false);     yearBuf = new BufferedWriter(yearWriter);     monthWriter = new java.io.FileWriter(outputLocationFile + "\" + ncFile.getName().substring(0, ncFile.getName().lastIndexOf(".")) + "_modified_month.csv", false);     monthBuf = new BufferedWriter(monthWriter);     dayWriter = new java.io.FileWriter(outputLocationFile + "\" + ncFile.getName().substring(0, ncFile.getName().lastIndexOf(".")) + "_modified_day.csv", false);     dayBuf = new BufferedWriter(dayWriter);     String input;     String temp;     String temp2;     String temp3;     while ((input = br.readLine()) != null) {         temp = input.substring(0, 4);         temp2 = input.substring(4, 6);         temp3 = input.substring(6);         Platform.runLater(new Runnable() {  @Override  public void run() {      myIndicator.setProgress(-1);  }         });         yearBuf.write(temp + "n");         monthBuf.write(temp2 + "n");         dayBuf.write(temp3 + "n");         yearBuf.flush();         monthBuf.flush();         dayBuf.flush();         temp = null;         temp2 = null;         temp3 = null;     } } else {     fWriter = new java.io.FileWriter(outputLocationFile + "\" + ncFile.getName() + "_modified.csv", false);     writer = new BufferedWriter(fWriter);     String input;     String temp;     while ((input = br.readLine()) != null) {         temp = input.substring(0, 4) + "," + input.substring(4, 6) + "," + input.substring(6);         Platform.runLater(new Runnable() {  @Override  public void run() {      myIndicator.setProgress(-1);  }         });         writer.write(temp + "n");         writer.flush();     } }        } catch (IOException e) { e.printStackTrace(System.out);        } finally { try {     if (br != null) {         br.close();     }     if (yearBuf != null) {         yearBuf.close();     }     if (monthBuf != null) {         monthBuf.close();     }     if (dayBuf != null) {         dayBuf.close();     }     if (yearWriter != null) {         yearWriter.close();     }     if (monthWriter != null) {         monthWriter.close();     }     if (dayWriter != null) {         dayWriter.close();     }     if (fWriter != null) {         fWriter.close();     }     if (writer != null) {         writer.close();     } } catch (IOException e) {     e.printStackTrace(System.out); }        }        return true;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508093.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号