我正在尝试使用Java来获取当前运行的Java虚拟机所使用的CPU百分比.我的研究指出我使用这门com.sun.management.OperatingSystemMXBean课程.以下是在线示例,我写了以下内容:
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
public class TestClass {
public static void main (String[] args) {
OperatingSystemMXBean bean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
System.out.println(bean.getProcessCpuLoad());
System.out.println(bean.getSystemCpuLoad());
}
}
Run Code Online (Sandbox Code Playgroud)
在两台机器上测试后,我无法停止获得-1.0两个调用的结果.我尝试通过各种方法使用64位和32位版本的Java 7和6来运行此代码.不过,我得到的是打印出的-1.0该,根据的Javadoc,
All values betweens 0.0 and 1.0 are possible depending of the activities going on in the system. If the system recent cpu usage is not available, the method returns a negative value.
我不知道这里还能做什么.获得CPU负载至关重要,我想避免使用像Sigar这样的其他库.有人对此问题有任何建议或有任何见解吗?
请原谅我,如果这个问题主要是基于意见的,但我觉得它不是,而且选择的理由很充分.所以,这是一个例子.对不起,它真的很长,但超级简单:
接口:
public interface Shape
{
double area ();
}
Run Code Online (Sandbox Code Playgroud)
实施第1课:
import static java.lang.Math.PI;
public class Circle implements Shape
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public double area()
{
return PI*radius*radius;
}
}
Run Code Online (Sandbox Code Playgroud)
实施第2课:
public class Square implements Shape
{
private double size;
public Square(double sideLength)
{
size = sideLength;
}
public double area()
{
return size*size;
}
}
Run Code Online (Sandbox Code Playgroud)
司机:
Shape[] shapes = new Shape[]{new Circle (5.3), new Square (2.4)};
System.out.println(shapes[0].area()); //prints 88.247...
System.out.println(shapes[1].area()); …Run Code Online (Sandbox Code Playgroud) 是否有一种标准技术来"扩展"具有私有构造函数的类,就像大多数Singleton类一样?具体来说,我正在尝试扩展java.lang.management.ThreadInfo该类,因为我正在添加很多它们HashSet来控制唯一性.但是,我确定两个线程是否相等的方式是不同的,并且与方法的默认实现不同equals().
在这种情况下,扩展类显然不是一个选项.
制作类似于ThreadInfo在构造函数中接受a的包装类,然后使用值手动填充所有相关字段,然后覆盖equals()和hashCode()/或者有更好的方法来执行此操作是否合理?
像我这样的东西就是我开始写的东西,但更好的实现是理想的:
class ThreadInfoWrapper {
private ThreadInfo info;
ThreadInfoWrapper(ThreadInfo info) {
this.info = info;
}
//Populate instance variables with Thread.State, thread ID, etc.. with
//Getters/setters and all that other stuff
public boolean equals(Object o) { //Unique implementation
}
public int hashCode() { //Whatever implementation
}
}
Run Code Online (Sandbox Code Playgroud)
但这感觉就像是一种非常迂回的方式来实现一些基本功能.我调查了一下,Java标准库中不存在使用自定义比较器的集合的实现.我想我可以编写自己的哈希集实现,但这对于一个简单的情况来说太过分了.任何见解都会有所帮助.
我正在浏览类的源代码,java.util.HashMap并注意到显式的no-arg构造函数需要两个常量:
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
public HashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
Run Code Online (Sandbox Code Playgroud)
但是当我查看DEFAULT_INITIAL_CAPACITY常量时,我发现它的定义如下:
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
Run Code Online (Sandbox Code Playgroud)
我从未见过我使用过的任何产品中使用的这种类型的构造,并且在Java语言规范或谷歌搜索中找不到任何结果.所以我查看了字节代码,但我发现使用16vs 1 << 4提供了相同的输出,这意味着(至少在我的极简主义情况下)编译器会将后者转换为十进制表示法.两个版本的字节码包括以下定义:
javap -c -verbose /---/myClass.class
----
public static final int i;
flags: ACC_PUBLIC, ACC_STATIC, ACC_FINAL …Run Code Online (Sandbox Code Playgroud) 情况。我们正在使用 Spring OAuth 来验证安全令牌 (JWT)。令牌具有aud对特定资源 ID 的声明。以下代码正确验证使用包含客户端 ID 的 aud 声明签名的任何 JWT 令牌resourceId-123:
class ResourceServerConfig {
@Bean
protected ResourceServerConfiguration adminResources2() {
ResourceServerConfiguration resource = new ResourceServerConfiguration() {
void setConfigurers(List<ResourceServerConfigurer> configurers) {
super.setConfigurers(configurers)
}
}
resource.setConfigurers(Collections.<ResourceServerConfigurer>singletonList(new ResourceServerConfigurerAdapter() {
@Override
void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("resourceId-123")
}
@Override
void configure(HttpSecurity http) throws Exception {
http.antMatcher("/path")
.authorizeRequests()
.anyRequest().authenticated()
}
}))
resource.setOrder(3)
return resource
}
}
Run Code Online (Sandbox Code Playgroud)
题。我们如何支持同一路径的多个客户端 ID(在上面的示例中,/path)?我已经看到有关如何为不同的API 路径配置具有不同客户端 ID 的多个 Bean 的示例,但我想使用 …
如果我表演
97346822*3f, result is 2.9204048E8,
Run Code Online (Sandbox Code Playgroud)
然而
97346822*3.0 gives me 2.92040466E8.
Run Code Online (Sandbox Code Playgroud)
请解释.
我正在尝试创建一个while循环,但编译器一直说我有一个"非法的类型启动".我该如何解决?
码:
class whileLoop
{
int p = 0;
while(p < 10)
{
System.out.println(p);
p++;
}
}
Run Code Online (Sandbox Code Playgroud) 需要从上午 8 点到下午 3:30 每 3 分钟安排一次作业。我知道如何从早上 8 点到下午 3 点进行。如何让它持续到下午 3:30?
我试图从设备外部目录中读取.json文件.
我有一个名为ExternalFile的类,用于读取文件并将内容作为字符串返回.
这是班级:
public class ExternalFile
{
final String EXTERNAL_STORAGE = Evironment.getExternalStorageDirectory().toString();
final String FIRSTDROID_DIRECTORY = EXTERNAL_STORAGE + "/firstdroid/";
final String SALES_DIRECTORY = FIRSTDROID_DIRECTORY + "sales/";
final String REFERENCE_DIRECTORY = FIRSTDROID_DIRECTORY + "reference/";
public String readFile(String direcectory, String fileName)
{
BufferedReader br;
StringBuilder sBuffer;
File JSON;
String line;
String retVal = null;
try
{
sBuffer = new StringBuilder();
JSON = new File(direcectory, fileName);
br = new BufferedReader(new FileReader(JSON));
while ((line = br.readLine()) != null)
{
sBuffer.append(line);
sBuffer.append('\n');
}
retVal = …Run Code Online (Sandbox Code Playgroud) java ×8
android ×1
bit-shift ×1
constants ×1
cpu ×1
cron ×1
double ×1
equals ×1
hash ×1
inheritance ×1
jls ×1
json ×1
jwt ×1
linux ×1
literals ×1
oauth-2.0 ×1
overloading ×1
overriding ×1
spring ×1
while-loop ×1