以下处理方式有什么区别InterruptedException?最好的方法是什么?
try{
//...
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
Run Code Online (Sandbox Code Playgroud)
要么
try{
//...
} catch(InterruptedException e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
编辑:我也想知道这两个使用的场景.
java multithreading exception-handling interrupted-exception
如果我在同一个类上同步两个方法,它们可以同时在同一个对象上运行吗?例如:
class A {
public synchronized void methodA() {
//method A
}
public synchronized void methodB() {
// method B
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我不能methodA()在两个不同的线程中对同一个对象运行两次.同样的事情methodB().
但是,在运行methodB()时我可以在不同的线程上methodA()运行吗?(同一个对象)
有没有一种简单的方法将LocalDate(用Java 8引入)转换为java.util.Date对象?
通过'简单',我的意思是比这更简单:
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎有点尴尬.
由于我们只对日期部分感兴趣,并且两个对象中都没有时区信息,为什么要明确引入时区?应该隐式采用午夜时间和系统默认时区进行转换.
Java的Thread.sleep什么时候抛出InterruptedException?忽视它是否安全?我没有做任何多线程.我只是想等几秒钟再重试一些操作.
java multithreading sleep interrupted-exception interruption
我有一个名为的类Media,它有一个名为的方法setLoanItem:
public void setLoanItem(String loan) {
this.onloan = loan;
}
Run Code Online (Sandbox Code Playgroud)
我试图从以GUI下列方式命名的类中调用此方法:
public void loanItem() {
Media.setLoanItem("Yes");
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了错误
非静态方法setLoanItem(java.lang.String)不能从静态上下文引用
我只是试图onloan将Media类中的变量更改为"是" GUI.
我看过其他主题有相同的错误消息,但没有点击!
首先,我在StackOverflow上发现了很多关于此的线程,但是没有一个真正帮助过我,所以很抱歉可能会重复提问.
我正在使用spring-test运行JUnit测试,我的代码看起来像这样
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {})
public class StudentSystemTest {
@Autowired
private StudentSystem studentSystem;
@Before
public void initTest() {
// set up the database, create basic structure for testing
}
@Test
public void test1() {
}
...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我希望我的测试不会影响其他测试.所以我想为每个测试创建类似回滚的东西.我为此搜索了很多,但到目前为止我一无所获.我正在使用Hibernate和MySql
如何获得BigDecimal变量的最大可能值?(最好是以编程方式,但硬编码也可以)
编辑
好了,刚才意识到没有这样的东西,因为BigDecimal是任意精度.所以我最终得到了这个,这对我的目的来说足够好了:
BigDecimal my = BigDecimal.valueOf(Double.MAX_VALUE)
我是Spring3.x的初学者,我正在学习Spring DAO的支持.我想知道NamedParameterJdbcTemplate和JdbcTemplate之间的区别.通过表现哪一个是最好的.什么时候去NamedParameterJdbcTemplate,什么时候去JdbcTemplate.你的答案对我这样的初学者有很大的帮助.
如何根据包名设置log4j以记录到不同的文件?
例如:
com.myname.fred logs to fred.log
com.myname.derek logs to derek.log
Run Code Online (Sandbox Code Playgroud)
我正在使用属性文件配置格式.目前它是为服务器设置的,我想单独留下它并为我的日志记录添加内容.
以下是属性文件的外观:
##
## 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 …Run Code Online (Sandbox Code Playgroud) 据我所知,Java 8 JLS表达式(1/0)被认为是一个常量表达式,但是当我尝试使用OpenJDK 8编译以下程序时出现错误
public class Switch {
public static void main(String[] args) {
switch(42) {
case (1/0):
return;
default:
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误说(1/0)不是一个常量表达式
Switch.java:4: error: constant expression required
case (1/0):
^
1 error
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?或者它是OpenJDK 8中的错误?
java ×10
java-8 ×2
spring ×2
bigdecimal ×1
date ×1
hibernate ×1
interruption ×1
java-ee ×1
java-threads ×1
java-time ×1
jdbc ×1
jdbctemplate ×1
jls ×1
junit ×1
log4j ×1
math ×1
openjdk ×1
sleep ×1
spring-jdbc ×1
spring-test ×1
static ×1
synchronized ×1