我遇到过EntityUtils.consume(httpEntity);,我不确定它到底是做什么的.
例如:
try {
//... some code
HttpEntity httpEntity = httpResponse.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(http.Entity.getContent()));
String line;
while ((line = br.readLine())!= null) {
System.out.println(line);
}
EntityUtils.consume(httpEntity);
} catch (Exception e) {
//code
} finally {
httpClient.getConnectionManager().shutdown();
}
Run Code Online (Sandbox Code Playgroud)
为什么作者在块关闭连接EntityUtils.consume(httpEntity);时放入finally并且垃圾收集器会处理httpEntity?
情况如下:
问题是:我是否必须使此类的所有变量都不稳定?
关注:
注意:我知道构建器模式,但由于其他几个原因我无法应用它:(
编辑: 由于我觉得Mathias和axtavt的两个答案不太匹配,我想补充一个例子:
假设我们有一个foo班级:
class Foo {
public int x=0;
}
Run Code Online (Sandbox Code Playgroud)
并且两个线程正在使用它,如上所述:
// Thread 1 init the value:
Foo f = new Foo();
f.x = 5;
values.add(f); // Publication via thread-safe collection like Vector or Collections.synchronizedList(new ArrayList(...)) or ConcurrentHashMap?.
// Thread 2
if (values.size()>0){
System.out.println(values.get(0).x); // always 5 ?
}
Run Code Online (Sandbox Code Playgroud)
据我所知,Mathias可以根据JLS在某些JVM上打印出0.据我所知,它将始终打印5.
你有什么意见?
- 问候,德米特里
来自Java背景,我知道这不会编译.
public static class SuperClass {}
public static class SubClass extends SuperClass {}
public static <T, U extends T> U returnSub(T sup, U sub) {
return sub;
}
public static void main(String[] args) {
SuperClass parent = new SuperClass();
SubClass child = new SubClass();
returnSub(parent, child);
returnSub(child, parent); // Java doesn't like that
}
Run Code Online (Sandbox Code Playgroud)
最后一行产生编译器错误(编辑:至少在jdk1.6.0_65上),它确实:
绑定不匹配:类型Test的泛型方法returnSub(T,U)不适用于参数(Test.SubClass,Test.SuperClass).推断类型Test.SuperClass不是有界参数的有效替代
所以,我很惊讶,这似乎在Scala中起作用.我在下面编写了示例代码(据我所知,表达了相同的"逻辑"):
class SuperClass
class SubClass extends SuperClass
def returnSub[Type, SubType <: Type](supArg: Type, subArg: SubType): SubType = {
subArg
}
override def main(args: Array[String]): …Run Code Online (Sandbox Code Playgroud) 我尝试做的是使用 XML 解析器解析字符串。
我只找到这种在 Scala 中使用 Spark 进行解析的方法:
val df = sqlContext
.read
.format("com.databricks.spark.xml")
.option("rowTag", "book")
.load("books.xml")
Run Code Online (Sandbox Code Playgroud)
我需要解析的是一个字符串,而不是一个文件
那么,是否有任何选项可以加载字符串(而不是文件路径)?
谢谢!
我有一个 Web 服务,我在其中使用客户端 API 中的服务接口对象操作 SOAP 标头。
我需要将端口对象类型转换为BindingProvider对象。但是我的端口对象没有直接继承那个类。那么JVM怎么可能不抱怨呢?
它也有效。ClassCastException 没有运行时错误
代码片段:
public SearchDocument getSearchDocumentService(String wsdlUri, AuthBean auth){
SearchDocument_Service serv = null;
serv = SearchDocument_Service.getServiceInstance(wsdlUri);
SearchDocument searchDoc = serv.getSearchDocument();
populateAuthAndHandlerInfo((BindingProvider)searchDoc, auth);//how is it that jvm doesn't complain here
return searchDoc;
}
private void populateAuthAndHandlerInfo(BindingProvider port, AuthBean auth) {
Binding binding = port.getBinding();
List<Handler> handlerList = binding.getHandlerChain();
handlerList.add(new EDMSSoapAuthHandler());
binding.setHandlerChain(handlerList);
Map<String, Object> context = port.getRequestContext();
context.put("clientAuthInfo", auth);
}
Run Code Online (Sandbox Code Playgroud)
搜索文档.java:
@WebService(name = "SearchDocument", targetNamespace = "http://services.abc.com/Technology/SearchDocument/service/v1")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@XmlSeeAlso({
ObjectFactory.class …Run Code Online (Sandbox Code Playgroud) java ×4
scala ×2
apache-spark ×1
concurrency ×1
generics ×1
jakarta-ee ×1
jax-ws ×1
soap-client ×1
volatile ×1
web-services ×1
xml ×1