当使用Java进行编程同步时,我发现找不到我预期的用法.也就是说,在线程A中,它访问两个嵌套的同步块中的实例成员(内容):
synchronized(this){
synchronized (content) {content = str;}
}
Run Code Online (Sandbox Code Playgroud)
而在线程B中,它只访问一个同步块中的相同内容:
synchronized (content) {content = str;}
Run Code Online (Sandbox Code Playgroud)
我预计这可以作为正常使用
synchronized (content) {content = str;}
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.它的工作原理是没有同步.完整的代码和日志如下:
public class JavaSync {
public static void main(String[] args) {
SyncContent syncContent = new SyncContent("JavaSync");
ThreadA a = new ThreadA(syncContent);
a.setName("A");
a.start();
ThreadB b = new ThreadB(syncContent);
b.setName("B");
b.start();
ThreadC c = new ThreadC(syncContent);
c.setName("C");
c.start();
}
}
class SyncContent {
volatile String content = new String();
public SyncContent(String content) {
this.content = content;
}
private double timeConsuming() { …Run Code Online (Sandbox Code Playgroud) 我使用fabric8 kubernetes Java 客户端API 编写了一个示例来设置容器上的GPU 资源要求。我有以下运行时错误:
spec.containers[0].resources.requests[gpu]: Invalid value: "gpu": must be a standard resource type or fully qualified,
spec.containers[0].resources.requests[gpu]: Invalid value: "gpu": must be a standard resource for containers.
Run Code Online (Sandbox Code Playgroud)
fabric8 jar 的版本是 4.3.0(最新)。到目前为止,fabric8 似乎不支持 gpu 资源需求,当我删除“addToRequests("gpu", new Quantity("1"))”行时,它可以正常工作。
那么如何在 Java/Scala 应用程序中启用 GPU 资源需求呢?
该示例的整个源代码如下:
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may …Run Code Online (Sandbox Code Playgroud)