我正在编写一个实用程序方法,可以检查空字符串和空字符串,或集合或对象或任何常规类型 -
public static boolean isEmpty(Object obj) {
if (obj == null)
return true;
if (obj instanceof Collection)
return ((Collection<?>) obj).size() == 0;
// is below line expensive?
final String s = String.valueOf(obj).trim();
return s.length() == 0 || s.equalsIgnoreCase("null");
}
Run Code Online (Sandbox Code Playgroud)
如何使上述方法高效,因为上述isEmpty方法将从应用程序中多次调用,这对性能至关重要?
我怀疑下面的行是昂贵的,因为繁重的toString方法,它会创建临时垃圾,可能会导致GC并降低性能?
final String s = String.valueOf(obj).trim();
Run Code Online (Sandbox Code Playgroud)
更新: -
我现在为每种类型分离了isEmpty方法.以下是我在简化上述isEmpty方法后得到的结果.
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
return false;
}
public static boolean isEmpty(Collection<?> value) {
if (value == null || …Run Code Online (Sandbox Code Playgroud) 我有一个属性文件,就像这样 -
hostName=machineA.domain.host.com
emailFrom=tester@host.com
emailTo=world@host.com
emailCc=hello@host.com
Run Code Online (Sandbox Code Playgroud)
现在我正在从我的Java程序中读取上面的属性文件 -
public class FileReaderTask {
private static String hostName;
private static String emailFrom;
private static String emailTo;
private static String emailCc;
private static final String configFileName = "config.properties";
private static final Properties prop = new Properties();
public static void main(String[] args) {
readConfig(arguments);
}
private static void readConfig(String[] args) throws FileNotFoundException, IOException {
if (!TestUtils.isEmpty(args) && args.length != 0) {
prop.load(new FileInputStream(args[0]));
} else {
prop.load(FileReaderTask.class.getClassLoader().getResourceAsStream(configFileName));
}
hostName = prop.getProperty("hostName").trim();
emailFrom = prop.getProperty("emailFrom").trim(); …Run Code Online (Sandbox Code Playgroud) 我需要创建一个库,在其中我将具有同步和异步功能.
executeSynchronous() - 等到我有结果,返回结果.executeAsynchronous() - 立即返回Future,如果需要,可在其他事情完成后处理.我的图书馆的核心逻辑
客户将使用我们的库,他们将通过传递DataKey构建器对象来调用它.然后我们将使用该DataKey对象构造一个URL,并通过执行它来对该URL进行HTTP客户端调用,然后在我们将响应作为JSON字符串返回之后,我们将通过创建DataResponse对象将该JSON字符串发送回我们的客户.有些客户会调用executeSynchronous(),有些可能会调用executeAsynchronous()方法,这就是为什么我需要在库中单独提供两种方法.
接口:
public interface Client {
// for synchronous
public DataResponse executeSynchronous(DataKey key);
// for asynchronous
public Future<DataResponse> executeAsynchronous(DataKey key);
}
Run Code Online (Sandbox Code Playgroud)
然后我有我DataClient实现上面的Client接口:
public class DataClient implements Client {
private RestTemplate restTemplate = new RestTemplate();
private ExecutorService executor = Executors.newFixedThreadPool(10);
// for synchronous call
@Override
public DataResponse executeSynchronous(DataKey key) {
DataResponse dataResponse = null;
Future<DataResponse> future = null;
try …Run Code Online (Sandbox Code Playgroud) java performance multithreading executorservice resttemplate
我想在我的代码中添加一个JavaDoc.我需要在一次抛出中添加多个异常.当我在下面添加时,它只识别NullPointerException不是IllegalArgumentException.有什么方法可以在单个throw标签中提供多个异常,这样当我将鼠标放在方法上时它可以识别这两个异常吗?
@throws NullPointerException, IllegalArgumentException when invalid userId, timeout is passed
Run Code Online (Sandbox Code Playgroud)
或者我需要这样做?通过这个,我重复两次相同的评论.
@throws NullPointerException when invalid userId, timeout is passed
@throws IllegalArgumentException when invalid userId, timeout is passed
Run Code Online (Sandbox Code Playgroud) 我有以下Dockerfile用于zookeeper安装,我为此建立了docker镜像.
FROM openjdk:8-jre-alpine
# Install required packages
RUN apk add --no-cache \
bash \
su-exec
ENV ZOO_USER=zookeeper \
ZOO_CONF_DIR=/conf \
ZOO_DATA_DIR=/data \
ZOO_DATA_LOG_DIR=/datalog \
ZOO_PORT=2181 \
ZOO_TICK_TIME=2000 \
ZOO_INIT_LIMIT=5 \
ZOO_SYNC_LIMIT=2
# Add a user and make dirs
RUN set -x \
&& adduser -D "$ZOO_USER" \
&& mkdir -p "$ZOO_DATA_LOG_DIR" "$ZOO_DATA_DIR" "$ZOO_CONF_DIR" \
&& chown "$ZOO_USER:$ZOO_USER" "$ZOO_DATA_LOG_DIR" "$ZOO_DATA_DIR" "$ZOO_CONF_DIR"
ARG GPG_KEY=C823E3E5B12AF29C67F81976F5CECB3CB5E9BD2D
ARG DISTRO_NAME=zookeeper-3.4.9
# Download Apache Zookeeper, verify its PGP signature, untar and clean up
RUN set -x \ …Run Code Online (Sandbox Code Playgroud) 我使用datastax java驱动程序3.1.0连接到cassandra集群,我的cassandra集群版本是2.0.10.我正在以QUORUM一致性异步写作.
private final ExecutorService executorService = Executors.newFixedThreadPool(10);
private final Semaphore concurrentQueries = new Semaphore(1000);
public void save(String process, int clientid, long deviceid) {
String sql = "insert into storage (process, clientid, deviceid) values (?, ?, ?)";
try {
BoundStatement bs = CacheStatement.getInstance().getStatement(sql);
bs.setConsistencyLevel(ConsistencyLevel.QUORUM);
bs.setString(0, process);
bs.setInt(1, clientid);
bs.setLong(2, deviceid);
concurrentQueries.acquire();
ResultSetFuture future = session.executeAsync(bs);
Futures.addCallback(future, new FutureCallback<ResultSet>() {
@Override
public void onSuccess(ResultSet result) {
concurrentQueries.release();
logger.logInfo("successfully written");
}
@Override
public void onFailure(Throwable t) {
concurrentQueries.release();
logger.logError("error= ", …Run Code Online (Sandbox Code Playgroud) 我有一个进程,我想尽快开始我的系统rebooted,所以我使用upstart脚本,但有时我注意到的是我的进程在硬重启(插拔和启动机器)期间没有启动所以我认为我的暴发脚本在硬重启后没有被踢进去.我相信Hard Reboot没有运行级别.
我很困惑,为什么有时在重启期间它有效,但有时它不起作用.我该如何调试呢?
以下是我的新贵脚本:
# sudo start helper
# sudo stop helper
# sudo status helper
start on runlevel [2345]
stop on runlevel [!2345]
chdir /data
respawn
pre-start script
echo "[`date`] Agent Starting" >> /data/agent.log
sleep 30
end script
post-stop script
echo "[`date`] Agent Stopping" >> /data/agent.log
sleep 30
end script
limit core unlimited unlimited
limit nofile 100000 100000
setuid goldy
exec python helper.py
Run Code Online (Sandbox Code Playgroud)
有没有办法调试出来发生了什么?我相信,我可以很容易地重现这一点.关于我在这里可以做什么的任何指示?
注意:
在重新启动期间,有时我会在pre-start脚本中看到我的日志记录,但有时我在重新启动后根本看不到日志记录,这意味着我的upstart脚本未被触发.有什么我需要在运行级别上更改以使其工作?
我有一个在Hypervisor中运行的VM,我正在使用Ubuntu.
我正在开发一个基于自定义主题的WordPress网站,我想忽略一些来自wordpress插件样式表的特定CSS代码.
这是wordpress插件样式表的链接.
我想忽略的上述Wordpress插件样式表中的CSS代码是:
@media screen and (max-width: 575.98px) .gv-table-view tr:first-of-type {
border-top: 1px solid #ccc;
}
@media screen and (max-width: 575.98px) .gv-table-view tr {
display: block;
position: relative;
padding: 1.2em 0;
overflow-x: auto;
}
.gv-table-view th, .gv-table-view td {
padding: .3em;
}
@media screen and (max-width: 575.98px) .gv-table-view tr td {
display: table-row;
}
@media screen and (max-width: 575.98px) .gv-table-view tr td:before {
content: attr(data-label);
font-weight: bold;
display: table-cell;
padding: 0.2em 0.6em 0.2em 0;
text-align: right; …Run Code Online (Sandbox Code Playgroud) 我正在研究SQL查询代码,我想避免sql查询重复.
下面是sql查询代码:
switch ($y) {
case 'l.text':
$query->order('numeric_text ' . (strtolower($x) == 'DESC' ? 'DESC' : 'ASC') .
', ' . $y . ' ' . (strtolower($x) == 'DESC' ? 'DESC' : 'ASC'));
break;
}
Run Code Online (Sandbox Code Playgroud)
在上面的SQL查询代码strtolower($x) == 'DESC' ? 'DESC' : 'ASC'正在两个地方使用.我想在那里放置一个varibale.
这是我尝试过的:
$sortOrder = (strtolower($x) == 'DESC' ? 'DESC' : 'ASC');
switch ($y) {
case 'l.text':
$query->order('numeric_text ' . $sortOrder . ', ' . $y . ' ' . $sortOrder);
break;
}
Run Code Online (Sandbox Code Playgroud)
问题陈述:
我想知道是否还有其他更好的方法,我们可以避免sql查询重复.
我正在处理一个项目,在该项目中我需要调用我的服务,我的服务将以 JSON 格式返回数据。而且我不需要将此 JSON 响应序列化到任何 POJO,我只需要将数据作为字符串返回。这个应用程序对性能非常关键,所以HttpClient必须非常快
所以我决定使用 Apache HttpClient 还是有更好的替代方案可以使用?
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("some-url");
request.addHeader("Context", "some-value");
HttpResponse response = httpClient.execute(request);
String response = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
Run Code Online (Sandbox Code Playgroud)
但它抱怨说The type DefaultHttpClient is deprecated,也许他们有新版本HttpClient或其他方式来对 URL 进行 HttpClient 调用?
有什么我想念的吗?
java ×6
performance ×2
php ×2
boot ×1
cassandra ×1
command-line ×1
css ×1
docker ×1
dockerfile ×1
guava ×1
html ×1
httpclient ×1
javadoc ×1
linux ×1
null-check ×1
parsing ×1
properties ×1
resttemplate ×1
sql ×1
string ×1
throw ×1
ubuntu-12.04 ×1
ubuntu-14.04 ×1
upstart ×1
wordpress ×1