我是靠谱客的博主 幽默母鸡,最近开发中收集的这篇文章主要介绍linux命令设置休眠时间,让Java在循环之间休眠,在Linux上由命令行指定的休眠时间...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I have been assigned on designing a java program on a linux machine that:

我被指派在linux机器上设计一个java程序:

Connects to a database

连接到数据库

reads a record

读取记录

retrieve certain information and send to Nagios according to a field known as 'threat_level'

检索某些信息并根据称为“threat_level”的字段发送到Nagios

read next record and repeat step number 3 until all records have been read

读取下一条记录并重复步骤3,直到读完所有记录

Now, I needed to get this to run every few minutes; so what my partner did was create a script that uses a loop to run the program, sleeps a few minutes, and repeat.

现在,我需要每隔几分钟运行一次;所以我的合作伙伴所做的是创建一个脚本,使用循环来运行程序,睡几分钟,然后重复。

Recently, my boss told us that its good but would like the whole procedure to be completely self contained in java; meaning that it loops and sleeps within java. On top of that, he would like to have the sleep duration be determined by command line each time that the program is run.

最近,我的老板告诉我们它的好,但希望整个程序完全自包含在java中;意味着它在java中循环和休眠。最重要的是,他希望每次运行程序时都通过命令行确定睡眠持续时间。

I did some research and it seems that using Thread.sleep() is inefficient in certain circumstances and I cannot tell if this is one of them or not. Also, I am still unclear on how to have the sleep time be determined via command line upon running the program. I can provide the code if necessary.

我做了一些研究,似乎在某些情况下使用Thread.sleep()是低效的,我不知道这是否是其中之一。另外,我还不清楚如何在运行程序时通过命令行确定睡眠时间。如有必要,我可以提供代码。

4 个解决方案

#1

18

Thread.sleep() is just fine, especially when you want to sleep for "few minutes":

Thread.sleep()很好,特别是当你想要睡几个小时时:

public class Main {

public static void main(String[] args) throws InterruptedException {

final int sleepSeconds = Integer.parseInt(args[0]);

while(true) {

//do your job...

Thread.sleep(sleepSeconds * 1000);

}

}

}

Thread.sleep() might be inefficient or not precise enough in millisecond time ranges, but not in your case. But if you want the process to run in the same frequency (as opposed to with fixed delay), consider:

Thread.sleep()在毫秒时间范围内可能效率低或不够精确,但在您的情况下则不然。但是,如果您希望进程以相同的频率运行(而不是使用固定延迟),请考虑:

final long start = System.currentTimeMillis();

//do your job...

final long runningTime = System.currentTimeMillis() - start;

Thread.sleep(sleepSeconds * 1000 - runningTime);

This is important of "do your job" part might take significant amount of time and you want the process with exact frequency.

重要的是“做你的工作”部分可能需要花费大量时间,而且你希望这个过程具有准确的频率。

Also for readability consider TimeUnit class (uses Thread.sleep() underneath):

另外为了便于阅读,可以考虑使用TimeUnit类(在下面使用Thread.sleep()):

TimeUnit.SECONDS.sleep(sleepSeconds);

#2

1

Set a system property on the command line when the program is started: -Dmy.sleep.time=60000 Then get that parameter: long mySleepTime = System.getProperty("my.sleep.time");

程序启动时在命令行上设置系统属性:-Dmy.sleep.time = 60000然后获取该参数:long mySleepTime = System.getProperty(“my.sleep.time”);

Look at the Executor framework. The ScheduledExecutorService has a scheduleWithFixedDelay that will probably do what you want (run your code with a delay in between executions).

查看Executor框架。 ScheduledExecutorService有一个scheduleWithFixedDelay可能会执行您想要的操作(在执行之间延迟运行代码)。

#3

1

Take a look at the java.util.Concurrent API, in particular, you may be interested in the ScheduledExecutorService

看一下java.util.Concurrent API,特别是你可能对ScheduledExecutorService感兴趣

#4

0

When you run something in Java with a command-line arguments, they are stored in the main function's parameter args, as in:

当您使用命令行参数在Java中运行某些内容时,它们将存储在main函数的参数args中,如下所示:

public static void main(String[] args)

If you have only the one argument, and it is a number, you can turn it into an integer by using:

如果只有一个参数,并且它是一个数字,则可以使用以下方法将其转换为整数:

Integer.parseInt(args[0])

Which will return the integer value represented by the String stored as the zero element of the array args.

这将返回由存储为数组args的零元素的String表示的整数值。

EDIT: Note that you send command-line arguments when you invoke the Java Virtual Machine with:

编辑:请注意,在调用Java虚拟机时发送命令行参数:

java MyCompiledFile arg0 arg1 arg2

Where MyCompiledFile is the name of the file you want to run, and arg0, arg1, and arg2 (you can have as many arguments as you want) are the Strings that are going into args[0], args[1], and args[2] in the parameter passed to main. If you have multiple files in multiple directories, you'll need to specify a classpath - a directory which contains all the directories which contain your files. (Note that these are binary files, the result of compiling source files, not the source files themselves). In that case, use:

其中MyCompiledFile是您要运行的文件的名称,而arg0,arg1和arg2(您可以拥有任意数量的参数)是进入args [0],args [1]和args的字符串[2]在传递给main的参数中。如果多个目录中有多个文件,则需要指定一个类路径 - 一个包含所有包含文件的目录的目录。 (请注意,这些是二进制文件,编译源文件的结果,而不是源文件本身)。在这种情况下,使用:

java -cp MyClassPath MyCompiledFile arg0 arg1 arg2

Where MyClassPath is the Linux classpath (such as /home/usr/bin).

其中MyClassPath是Linux类路径(例如/ home / usr / bin)。

最后

以上就是幽默母鸡为你收集整理的linux命令设置休眠时间,让Java在循环之间休眠,在Linux上由命令行指定的休眠时间...的全部内容,希望文章能够帮你解决linux命令设置休眠时间,让Java在循环之间休眠,在Linux上由命令行指定的休眠时间...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部