我想创建一个副本,java.time.LocalDateTime但它没有clone()方法.
我所做的是以下内容:
long epochMilli = Instant.now().toEpochMilli();
LocalDateTime createDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
LocalDateTime modificationDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
Run Code Online (Sandbox Code Playgroud)
有没有最简单的方法来创建具有完全相同的日期时间值的两个LocalDateTime对象?
我正在读Brian Goetz的一本书"实践中的Java并发".第3.5和3.5.1段包含我无法理解的陈述.
请考虑以下代码:
public class Holder {
private int value;
public Holder(int value) {
this.value = value;
}
public void assertValue() {
if (value != value) throw new AssertionError("Magic");
}
}
class HolderContainer {
// Unsafe publication
public Holder holder;
public void init() {
holder = new Holder(42);
}
}
Run Code Online (Sandbox Code Playgroud)
作者说:
因此,根据文本,有些不幸的时机可能值= 0; 在下一刻,值= 42.
我同意第1点,Object构造函数首先使用默认值填充字段.但我不明白第2和第3点.
让我们更新作者代码并考虑以下示例:
public class Holder {
int value;
public Holder(int value) {
//Sleep to prevent constructor to finish too early
try { …Run Code Online (Sandbox Code Playgroud) 输入List<Item>按分数排序,Item如下所示:
class Item {
double score;
String category;
String author;
String poi;
}
Run Code Online (Sandbox Code Playgroud)
现在我需要在这些限制下从数组中选择10个得分最高的元素:
poiauthorcategory.并且来自同一子序列的任何子序列的长度category不应超过2.如果没有满足上述规则的子序列,只需返回前10个元素.
现在,我直接迭代List,并用三个HashMap<String, Integer>来存储每个的外观cagetory/poi/author.我List<Item> selected用来存储结果.
poi则将丢弃新元素.author则将丢弃新元素.category则将丢弃新元素.selected具有此category,那么新元素将被丢弃.它在输入很大时有效,但是当输入相对较小时,它不起作用.例如,输入时
那我的解决方案就是
Item3丢弃,因为它具有相同category的Item1和Item2Item4丢弃,因为它有相同 …我有一个String包含2或3个公司名称,每个名称都括在括号中.每个公司名称也可以包含括号中的单词.我需要使用正则表达式将它们分开,但没有找到.
我的inputStr:
(Motor (Sport) (racing) Ltd.) (Motorsport racing (Ltd.)) (Motorsport racing Ltd.)
or
(Motor (Sport) (racing) Ltd.) (Motorsport racing (Ltd.))
Run Code Online (Sandbox Code Playgroud)
预期的结果是:
str1 = Motor (Sport) (racing) Ltd.
str2 = Motorsport racing (Ltd.)
str3 = Motorsport racing Ltd.
Run Code Online (Sandbox Code Playgroud)
我的代码:
String str1, str2, str3;
Pattern p = Pattern.compile("\\((.*?)\\)");
Matcher m = p.matcher(inputStr);
int index = 0;
while(m.find()) {
String text = m.group(1);
text = text != null && StringUtils.countMatches(text, "(") != StringUtils.countMatches(text, ")") ? text + ")" : …Run Code Online (Sandbox Code Playgroud) 说我有一个方法:
public void run(){
synchronized(this.foo){
}
}
Run Code Online (Sandbox Code Playgroud)
但有时当我运行这个方法时,我不需要同步任何东西.
什么是条件同步的好模式?我能想到的唯一模式是回调,类似这样:
public void conditionalSync(Runnable r){
if(bar){
r.run();
return;
}
synchronized(this.foo){
r.run();
}
}
public void run(){
this.conditionalSync(()->{
});
}
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以做到吗,没有回调?
例如:
public synchronized Object get() {
while (result == null) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
// Do we own the monitor of this object?
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
当e.printStackTrace()执行,是我们保证自己的对象的显示器呢?
引用说当wait()在一个notify()或者notifyAll()调用之后返回时,线程会等待它获取对象的监视器.但wait()抛出异常时的情况呢?
我在nginx后面有五个tomcat实例。
有时nginx upstream_response_time很大,超过1秒,而tomcat本地访问日志显示处理时间仅为50ms(我%D用来记录处理时间)。
可能的原因是什么?如何解决?由于其他应用程序运行很快,网络似乎并不慢。
更新:
似乎nginx upstream_response_time= %D+ 1 sec。
我想用"字符串替换^.
String str = "hello \"there";
System.out.println(str);
String str1 = str.replaceAll("\"", "^");
System.out.println(str1);
String str2= str1.replaceAll("^", "\"");
System.out.println(str2);
Run Code Online (Sandbox Code Playgroud)
输出是:
hello "there
hello ^there
"hello ^there
Run Code Online (Sandbox Code Playgroud)
为什么我"在字符串的开头和字符串^之间得到额外的
我期待:
hello "there
Run Code Online (Sandbox Code Playgroud) 我有一个Hive表,它由列分区dt.如果不存在,我需要添加一个分区,例如,dt='20181219'.
现在我正在使用HiveMetaStoreClient#getPartition(dbName, tableName, 20181219).如果分区不存在,则捕获NoSuchObjectException并添加它.
在Java中有没有优雅的方法来实现这一目标?
我刚刚根据新的(ish)java 8时间包将我的许多日期转换为LocalDateTime.到目前为止,我一直很喜欢切换,直到我开始尝试序列化和反序列化.
如何配置Jackson支持他们?:
LocalDateTime --serialize - > UTC Timestamp --deserialize - > LocalDateTime?
这里有很多关于转换为格式化字符串的材料,但我似乎无法找到utc时间戳的开箱即用解决方案.