小编Max*_*lov的帖子

为什么T在Collections.max()签名中受Object限制?

刚刚完成了Java 7的java.util.Collections类的实现,看到了一些我不理解的东西.在max函数签名中,为什么T受限于Object

public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
    Iterator<? extends T> i = coll.iterator();
    T candidate = i.next();

    while (i.hasNext()) {
        T next = i.next();
        if (next.compareTo(candidate) > 0)
            candidate = next;
    }
    return candidate;
} 
Run Code Online (Sandbox Code Playgroud)

max 如果省略Object绑定似乎工作正常.

public static <T extends Comparable<? super T>> T max(Collection<? extends T> coll) {
    Iterator<? extends T> i = coll.iterator();
    T candidate = i.next();

    while (i.hasNext()) …
Run Code Online (Sandbox Code Playgroud)

java generics

69
推荐指数
1
解决办法
1773
查看次数

在多语言操作系统上使用Hyperic SIGAR时出现"java.library.path中没有sigar-x86-winnt.dll"错误

我在我的安装程序中使用Hyperic SIGAR库作为第三方库.我的安装程序将所有第三个lib文件解压缩到%TEMP%\\ user文件夹.

在英语操作系统上,一切都很好,但当我尝试在西班牙语Os上运行我的安装程序时,我遇到了以下错误:

java库包含sigar.jar:

java.class.path = C:\ DOCUME~1\西班牙语字母\CONFIG~1\Temp\e4j58.tmp_dir\user\sigar.jar

我的安装程序支持WinXP,WIN7 OS.

错误是:

no sigar-x86-winnt.dll in java.library.path
org.hyperic.sigar.SigarException: no sigar-x86-winnt.dll in java.library.path
at org.hyperic.sigar.Sigar.loadLibrary(Sigar.java:172)
at org.hyperic.sigar.Sigar.<clinit>(Sigar.java:100)
at I4jScript_Internal_1.eval(I4jScript_Internal_1.java:23)
at I4jScript_Internal_1.evaluate(I4jScript_Internal_1.java:79)
at com.install4j.runtime.installer.helper.Script.evaluate(Unknown Source)
at com.install4j.runtime.installer.ContextImpl.runScript(Unknown Source)
at com.install4j.runtime.installer.ContextImpl.runScript(Unknown Source)
at com.install4j.runtime.beans.actions.control.RunScriptAction.execute(Unknown Source)
at com.install4j.runtime.beans.actions.SystemInstallOrUninstallAction.install(Unknown Source)
at com.install4j.runtime.installer.InstallerContextImpl.performActionInt(Unknown Source)
at com.install4j.runtime.installer.ContextImpl.performAction(Unknown Source)
at com.install4j.runtime.installer.controller.Controller.executeActions(Unknown Source)
at com.install4j.runtime.installer.controller.Controller.handleCommand(Unknown Source)
at com.install4j.runtime.installer.controller.Controller.handleStartup(Unknown Source)
at com.install4j.runtime.installer.controller.Controller.start(Unknown Source)
at com.install4j.runtime.installer.Installer.main(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown …
Run Code Online (Sandbox Code Playgroud)

multilingual sigar

5
推荐指数
1
解决办法
8059
查看次数

我的线程获取了读锁,但是在尝试释放它时抛出了 IllegalMonitorStateException

我的应用程序部署在带有 java 6 update 30 的 Weblogic 10.3.5 上。我在执行此代码行时遇到以下错误:

    lock.readLock().lock();
    try {
        holder = cache.get(configName);
        // If it exists in the cache, return it
        if (holder != null)
            return holder;
    } finally {
        lock.readLock().unlock();
    }
Run Code Online (Sandbox Code Playgroud)

在类加载期间初始化锁:

private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
Run Code Online (Sandbox Code Playgroud)

缓存是:

private Map<String, ConfigurationHolder> cache = new HashMap<String,ConfigurationHolder>();
Run Code Online (Sandbox Code Playgroud)

突然抛出了 IllegalMonitorStateException:

Caused by: java.lang.IllegalMonitorStateException
    at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryReleaseShared(ReentrantReadWriteLock.java:363)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.releaseShared(AbstractQueuedSynchronizer.java:1317)
    at java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.unlock(ReentrantReadWriteLock.java:745) 
Run Code Online (Sandbox Code Playgroud)

在这里阅读与此场景类似的描述。

任何人都知道为什么会发生这种情况?

java exception reentrantreadwritelock

5
推荐指数
0
解决办法
1871
查看次数

Spring 已弃用的 ExpressionEvaluationUtils 评估方法的替代方法?

由于类:org.springframework.web.util.ExpressionEvaluationUtils 自 Spring 3.x 版本以来已被弃用,我正在我的 java 代码中寻找 EL 表达式评估的另一种替代方法。

阅读 JSP 2.x 文档后,我确实设法构建了一个替代方法:

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.servlet.jsp.JspApplicationContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;


public class ExpressionEvaluationUtils {


    public static Object evaluate(String exp, Class<?> resultClass, PageContext pageContext)
        throws JspException {

        if (pageContext == null){
            return exp;
        }
        ELContext elContext =  pageContext.getELContext();
        JspFactory jf = JspFactory.getDefaultFactory();
        JspApplicationContext jac = jf
                         .getJspApplicationContext(pageContext.getServletContext());
        ExpressionFactory ef = jac.getExpressionFactory();
        ValueExpression val = ef.createValueExpression(elContext, exp, resultClass);
        return val.getValue(elContext);
    }

}
Run Code Online (Sandbox Code Playgroud)

您能否为我的实现提出更多替代方案?

java spring jsp

5
推荐指数
1
解决办法
4012
查看次数

Java 8 性能的经典单例与惰性

最近读了一篇文章《Be Lazy With Java 8》 ”,其中介绍了一种创建惰性对象(在第一次访问时创建其内部状态的对象)的方法。

\n\n
public final class Lazy<T> {\n\n    private volatile T value;\n\n    public T getOrCompute(Supplier<T> supplier){\n        final T result = value;\n        return result == null ? maybeCompute(supplier) : result;\n    }\n\n    private synchronized T maybeCompute(Supplier<T> supplier) {\n        if (value == null){\n            value = Objects.requireNonNull(supplier.get());\n        }\n        return value;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我发现这个模式与众所周知的单例模式非常相似,除了泛型之外:

\n\n
public class PropertiesSingleton {\n\n    public static Properties getProperties(){\n        return Helper.INSTANCE;\n    }\n\n    private final static class Helper{\n        private final static Properties INSTANCE = computeWithClassLoaderLock();\n\n\n        private static …
Run Code Online (Sandbox Code Playgroud)

performance java-8 jmh

5
推荐指数
1
解决办法
2172
查看次数

Spring云配置客户端属性未得到解决

我对Spring Cloud和Spring外部配置的概念很陌生,实际上是昨天开始的.

我创建了一个Config Server从本地Git存储库中挑选配置,一个微服务也是配置客户端和一个Eureka驱动的服务发现服务器.

以下是我主要从互联网上的各种资源中借用的代码 -

配置服务器 - application.yml:

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: file:///${user.home}/config-repo
Run Code Online (Sandbox Code Playgroud)

配置服务器 - 主类(引导程序)

@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudConfigServerApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

config-repo是我机器上的本地git repo,并且有一个.yml文件,其名称为config client application,即authmanager.yml

eureka:
    client:
        serviceUrl:
            defaultZone: http://127.0.0.1:8761/eureka/
        healthcheck:
            enabled: true
        lease:
            duration: 5
spring:
    application:
        data:   
            mongodb:
                host: localhost
                port: 27017
                database: edc_mc
logging:
    level:
        org.exampledriven.eureka.customer.shared.CustomerServiceFeignClient: FULL
Run Code Online (Sandbox Code Playgroud)

现在运行配置服务器后,下面是端点http:// localhost:8888/authmanager/default的输出 -

{"name":"authmanager","profiles":["default"],"label":"master","version":"0ca6ca7b4502b9bb6ce1bf8efeb25516682cf79a","propertySources":[{"name":"file:///C:\\Users\\username/config-repo/authmanager.yml","source":{"eureka.client.serviceUrl.defaultZone":"http://127.0.0.1:8761/eureka/","eureka.client.healthcheck.enabled":true,"eureka.client.lease.duration":5,"spring.application.data.mongodb.host":"localhost","spring.application.data.mongodb.port":27017,"spring.application.data.mongodb.database":"db_name","logging.level.org.exampledriven.eureka.customer.shared.CustomerServiceFeignClient":"FULL"}}]}
Run Code Online (Sandbox Code Playgroud)

微服务+配置客户端代码 -

bootstrap.yml -

server:
  port: 9097

spring: …
Run Code Online (Sandbox Code Playgroud)

spring-boot spring-cloud spring-cloud-netflix spring-cloud-config

4
推荐指数
1
解决办法
5627
查看次数

用Java调度系统设计的消息

我正在为以下用例寻找轻量级和高效的解决方案:

  • 网关模块接收为不同接收者传递的资源.
  • 每个接受者排队的资源(按到达顺序).
  • 清除进程会扫描这些队列,如果资源可用于某些接受者,则他将它们捆绑在某个标记(唯一ID)下,并发送新捆绑可用的通知.

系统特点:

  • 接受者的数量是动态的.
  • 对一个包中的资源数量没有限制.

消息调度系统

该模块将在Java 7(非集群)下的Tomcat 7中使用.

我考虑了以下解决方案:

  1. JMS - 每个接受者的dymanic队列配置,是否可以使用队列中的所有可用消息?每个队列的线程配置(不可扩展)?
  2. AKKA演员.没有找到合适的使用模式.
  3. 朴素的纯Java实现,其中队列将由一个线程(循环)扫描.

我认为这是讨论此问题的可用解决方案的正确位置.请在考虑以下几点时分享您的想法:

  • 合适的第三方框架.
  • 资源队列可扩展扫描.

提前致谢.

java performance multithreading akka

2
推荐指数
1
解决办法
598
查看次数

登录JDK11 HttpClient

JDK11引入了新的HTTP Client,具有许多传统java.net.HttpURLConnection类所缺乏的功能。我遇到的第一个问题是如何在新添加的 HTTP 客户端中正确启用日志记录?

java logging http java-http-client java-11

2
推荐指数
1
解决办法
3801
查看次数