`

httpcore-4.4.12 BasicFuture 严重 bug【死锁】

    博客分类:
  • JAVA
 
阅读更多
 <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.12</version>
        </dependency>

 

    

/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package org.apache.http.concurrent;

import org.apache.http.util.Args;

import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Basic implementation of the {@link Future} interface. {@code BasicFuture}
 * can be put into a completed state by invoking any of the following methods:
 * {@link #cancel()}, {@link #failed(Exception)}, or {@link #completed(Object)}.
 *
 * @param <T> the future result type of an asynchronous operation.
 * @since 4.2
 */
public class BasicFuture<T> implements Future<T>, Cancellable {

    private final FutureCallback<T> callback;

    private volatile boolean completed;
    private volatile boolean cancelled;
    private volatile T result;
    private volatile Exception ex;

    public BasicFuture(final FutureCallback<T> callback) {
        super();
        this.callback = callback;
    }

    @Override
    public boolean isCancelled() {
        return this.cancelled;
    }

    @Override
    public boolean isDone() {
        return this.completed;
    }

    private T getResult() throws ExecutionException {
        if (this.ex != null) {
            throw new ExecutionException(this.ex);
        }
        if (cancelled) {
            throw new CancellationException();
        }
        return this.result;
    }

    @Override
    public synchronized T get() throws InterruptedException, ExecutionException {
        while (!this.completed) {
            wait();
        }
        return getResult();
    }

    @Override
    public synchronized T get(final long timeout, final TimeUnit unit)
            throws InterruptedException, ExecutionException, TimeoutException {
        Args.notNull(unit, "Time unit");
        final long msecs = unit.toMillis(timeout);
        final long startTime = (msecs <= 0) ? 0 : System.currentTimeMillis();
        long waitTime = msecs;
        if (this.completed) {
            return getResult();
        } else if (waitTime <= 0) {
            throw new TimeoutException();
        } else {
            for (;;) {
                wait(waitTime);
                if (this.completed) {
                    return getResult();
                }
                waitTime = msecs - (System.currentTimeMillis() - startTime);
                if (waitTime <= 0) {
                    throw new TimeoutException();
                }
            }
        }
    }

    public boolean completed(final T result) {
        synchronized(this) {
            if (this.completed) {
                return false;
            }
            this.completed = true;
            this.result = result;
            notifyAll();
        }
        if (this.callback != null) {
            this.callback.completed(result);
        }
        return true;
    }

    public boolean failed(final Exception exception) {
        synchronized(this) {
            if (this.completed) {
                return false;
            }
            this.completed = true;
            this.ex = exception;
            notifyAll();
        }
        if (this.callback != null) {
            this.callback.failed(exception);
        }
        return true;
    }

    @Override
    public boolean cancel(final boolean mayInterruptIfRunning) {
        synchronized(this) {
            if (this.completed) {
                return false;
            }
            this.completed = true;
            this.cancelled = true;
            notifyAll();
        }
        if (this.callback != null) {
            this.callback.cancelled();
        }
        return true;
    }

    @Override
    public boolean cancel() {
        return cancel(true);
    }

}

 

 

   




@Override
    public synchronized T get() throws InterruptedException, ExecutionException {
        while (!this.completed) {
            wait();
        }
        return getResult();
    }

public boolean completed(final T result) {
        synchronized(this) {
            if (this.completed) {
                return false;
            }
            this.completed = true;
            this.result = result;
            notifyAll();
        }
        if (this.callback != null) {
            this.callback.completed(result);
        }
        return true;
    }


都加上了synchronized

 

    这里会出现死锁

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    httpcore-4.4.12-API文档-中文版.zip

    赠送原API文档:httpcore-4.4.12-javadoc.jar; 赠送源代码:httpcore-4.4.12-sources.jar; 赠送Maven依赖信息文件:httpcore-4.4.12.pom; 包含翻译后的API文档:httpcore-4.4.12-javadoc-API文档-中文(简体)版....

    httpcore-4.4.12-API文档-中英对照版.zip

    赠送原API文档:httpcore-4.4.12-javadoc.jar; 赠送源代码:httpcore-4.4.12-sources.jar; 赠送Maven依赖信息文件:httpcore-4.4.12.pom; 包含翻译后的API文档:httpcore-4.4.12-javadoc-API文档-中文(简体)-英语...

    httpcore-nio-4.4.12-API文档-中文版.zip

    赠送原API文档:httpcore-nio-4.4.12-javadoc.jar; 赠送源代码:httpcore-nio-4.4.12-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.12.pom; 包含翻译后的API文档:httpcore-nio-4.4.12-javadoc-API文档...

    httpcore-4.4.12-sources.jar

    java运行依赖jar包

    httpcore-4.4.12.jar

    httpcore-4.4.12.jar文件实现了一系列的底层传输的功能 这些底层功能,可以用来去建立自己的client和server 支持两种I/O模式: 阻塞型Blocking:基于典型的Java的I/O模型 非阻塞型Non-Blocking:基于Java的NIO,...

    httpcore-nio-4.4.12-API文档-中英对照版.zip

    赠送原API文档:httpcore-nio-4.4.12-javadoc.jar; 赠送源代码:httpcore-nio-4.4.12-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.12.pom; 包含翻译后的API文档:httpcore-nio-4.4.12-javadoc-API文档...

    httpcore-nio-4.4.12.jar

    java运行依赖jar包

    httpcore-nio-4.4.12-sources.jar

    java运行依赖jar包

    mod_wsgi-windows-4.4.12

    2. **解压文件**: 解压缩下载的"mod_wsgi-windows-4.4.12"文件,通常会包含一个名为mod_wsgi-4.4.12-win32/或mod_wsgi-4.4.12-win64/的文件夹,取决于你的系统架构。 3. **复制mod_wsgi模块**: 将mod_wsgi*.dll文件...

    httpcore-4.4.12.jar中文-英文对照文档.zip

    注:下文中的 *** 代表文件名中的组件名称。 # 包含: 中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 ...

    httpcomponents-core-4.4.12-bin.zip

    《Apache HttpComponents Core 4.4.12:Java网络通信的核心组件》 Apache HttpComponents Core 是一个广泛使用的开源库,它是Java开发人员处理HTTP协议的基础。这个版本,4.4.12,包含了用于构建网络应用程序的核心...

    bugzilla-4.4.12.tar.gz.tar

    bugzilla-4.4.12.tar.gz.tar用于linux系统安装bugzilla程序

    httpcore.zip

    《HTTP核心库与HttpClient详解——基于httpcore-4.4.12.jar、Httpclient-4.5.10.jar和jettison-1.0.jar》 在现代的网络编程中,HTTP协议扮演着至关重要的角色。Apache的HTTP组件为开发者提供了强大的工具,使得处理...

    httpcore-nio-4.4.12.jar中文-英文对照文档.zip

    注:下文中的 *** 代表文件名中的组件名称。 # 包含: 中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 ...

    solr-core-4.4.0.JAR

    开发包 solr-core-4.4.0.jar,用于搜索引擎的搭建

    httpclient-4.5 jar包

    HttpClient 4.5还与其他Apache组件如HttpCore(httpcore-4.4.1.jar)紧密配合,HttpCore提供了基本的HTTP传输机制,包括套接字管理和HTTP协议解析。 在实际开发中,HttpClient 4.5能够很好地应对各种HTTP场景,如...

    swoole-src-4.4.12.tar.gz

    在本文中,我们将探讨Swoole 4.4.12版本的源码,并介绍如何从零开始编译安装这个强大的扩展。 首先,我们来解析一下标题"**swoole-src-4.4.12.tar.gz**"。这表明我们获取的是Swoole的源代码包,版本号为4.4.12,...

    kernel-headers-4.4.12-301.el7.x86_64.rpm

    有的系统安装后缺少这个rpm,导致后续软件不能安装 安装后可以正常编译驱动模块、程序等。

    httpcomponents-core-4.4.8-bin.zip

    《Apache HttpComponents Core 4.4.8:构建网络通信的核心框架》 Apache HttpComponents Core 是一个广泛使用的开源Java库,它为构建基于HTTP协议的网络应用提供了强大的支持。这个库是Apache HttpClient项目的一...

Global site tag (gtag.js) - Google Analytics