在jvm-prevent-tail-call-optimization之后两年,似乎有一个原型 实现,MLVM已经将该功能列为"proto 80%"一段时间了.
Sun的/ Oracle方面是否没有积极的兴趣支持尾调用,或者只是尾部调用" 在每个功能优先级列表中排在第二位 [...]"如JVM所述语言峰会?
如果有人测试了MLVM构建并且可以分享它的工作原理(如果有的话),我会非常感兴趣.
更新: 请注意,像Avian这样的某些虚拟机支持正确的尾部调用,没有任何问题.
java language-agnostic optimization jvm tail-call-optimization
这几乎看起来很傻但是关闭OutputStream时最可靠的模式是什么?现在我有类似下面的东西,似乎是try-catch-finally-overkill:
private void writeContentsToFile(OutputStream ostream, Properties contents) {
try {
contents.store(ostream, "comments");
}
catch (IOException e) {
throw new ResourceException("Failed to write contents", e);
}
finally {
try {
ostream.close();
}
catch (IOException e) { /* what can be done here anyway? */ }
}
}
Run Code Online (Sandbox Code Playgroud)
为什么近距离抛出一个经过检查的异常对我来说仍然是一个谜.我可以创建执行close/catch块的包装器方法,但是如果有某些东西已经存在,就像FileUtil.closeFileAndThrowUncheckedException()我想要使用它一样.当你有很多具有大量开发人员的小项目时,这会变得更有用; 一种方法是正确的.
通常,在处理Java IO代码时,这是我写的
FileOutputStream out = null;
try
{
out = new FileOutputStream("myfile.txt");
// More and more code goes here...
}
catch (Exception e)
{
}
finally
{
// I put the close code in finally block, to enture the opened
// file stream is always closed even there is exception happened.
if (out != null) {
// Another try catch block, troublesome.
try {
out.close();
} catch (IOException ex) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,当我尝试关闭文件流时,我需要处理另一个try ... catch块.
看起来很麻烦:(
有什么办法可以避免吗?将close代码放在非finally块中感觉不舒服,因为其他代码引起的异常将无法调用"close".
这是一个简单的TCP服务器.程序终止后如何关闭套接字?我使用try/finally并尝试关闭套接字.但是当我退出程序时它不会运行finally块.
任何人都可以知道如何以正确的方式关闭插座?
try {
socket = new ServerSocket(port);
System.out.println("Server is starting on port " + port + " ...");
}catch (IOException e){
System.out.println("Error on socket creation!");
}
Socket connectionSocket = null;
try{
while(true){
try{
connectionSocket = socket.accept();
Thread t = new Thread(new ClientConnection(connectionSocket));
t.start();
}catch (IOException e) {
System.out.println("Error on accept socket!");
}
}
}finally{
this.socket.close();
System.out.println("The server is shut down!");
}
Run Code Online (Sandbox Code Playgroud) 基本上,我想打开一个文件,读取一些字节,然后关闭该文件.这就是我想出的:
try
{
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
try
{
// ...
inputStream.read(buffer);
// ...
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
inputStream.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
也许我被RAII宠坏了,但是必须有更好的方法在Java中做到这一点,对吗?
在以下代码块中:
try ( /* resources declaration */ ) {
// some dangerous code
} catch (Exception e) {
// error handling and reporting
}
Run Code Online (Sandbox Code Playgroud)
如果会发生什么,都在里面的代码try块和自动close()声明抛出异常?哪一个会陷入困境catch?他们都?只有其中一个?如果是这样,哪一个?
如果try成功但close不是?是否会输入捕获块?
关闭一个当什么是最可靠的模式可供遵循OutputStream,ServerSocket或其他对象实现的AutoCloseable接口?
我应该使用try- catch-finally吗?或者关闭钩子。
我有自己的异常,由我的类抛出,BrowserException.
这可能是因为一些内部问题,即UnsupporderEncodingException.
现在我有两个选择:
在例外情况下,suppressException和cause之间有什么区别?
我什么时候更喜欢使用它们?
java ×8
exception ×2
io ×1
jvm ×1
optimization ×1
outputstream ×1
resources ×1
serversocket ×1
sockets ×1
tcp ×1
try-catch ×1