Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问老哥shutdown在哪儿调用呢 #2

Open
whoami-zy opened this issue Sep 19, 2019 · 1 comment
Open

请问老哥shutdown在哪儿调用呢 #2

whoami-zy opened this issue Sep 19, 2019 · 1 comment

Comments

@whoami-zy
Copy link

我再学习这个代码的时候,遇到主线程一直不退出。想请问下老哥shutdown是在哪儿呢。
我加在了await的后面。

@hp88yx
Copy link

hp88yx commented May 7, 2023

工具类里的shutdown的方法要改一下, 这是我改的代码, 改完之后可以在start()后,直接调用shutdown()方法, executorservice.shutdown()方法是优雅停止,会先判断任务是否执行完成, 任务未完成则会等待任务执行完成再终止线程.

`` public void shutdown() {
this.executorService.shutdown();
try {
// 等待所有线程执行完成才释放资源,防止读取到一半释放资源造成文件句柄丢失问题
for ( ; ;) {
if (executorService.isTerminated()) {
this.rAccessFile.close();
System.out.println("读取任务结束, 释放资源");
break;
}
// 停5秒进入下一次判断是否任务执行完成
Thread.sleep(5000);
}
} catch (Exception e) {
System.out.println("任务结束时, 关闭访问异常");
}
}

private static void testMultiThreadRead() {
    BigFileReader.Builder builder = new BigFileReader.Builder("D:\\soft\\OneDrive\\桌面\\fsdownload\\test.txt", new MyFileHandle());
    builder.withTreahdSize(2)
            .withCharset("utf-8")
            .withBufferSize(1024);
    BigFileReader bigFileReader = builder.build();
    bigFileReader.start();
    bigFileReader.shutdown();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants