任何人都可以解释以下代码String.java,特别是为什么有三个if语句(我已经标记//1,//2并且//3)?
private static class CaseInsensitiveComparator
implements Comparator<String>, java.io.Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 8575799808933029326L;
public int compare(String s1, String s2) {
int n1=s1.length(), n2=s2.length();
for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) {
char c1 = s1.charAt(i1);
char c2 = s2.charAt(i2);
if (c1 != c2) {/////////////////////////1
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {/////////////////////////2
c1 = …Run Code Online (Sandbox Code Playgroud) 考虑一下volatile int sharedVar.我们知道JLS为我们提供了以下保证:
w其写入值之前i以sharedVar程序顺序happens-before写入动作;i通过w happens-before的成功读取i从sharedVar由读取线程r;i从sharedVar由读线程r happens-before的所有后续行动r的程序顺序.然而,仍有给出没有挂钟时间的保证,当读线程将观察值i.一个完全不会让读取线程看到该值的实现仍然符合此契约.
我已经考虑了一段时间,我看不到任何漏洞,但我认为必须有.请指出我的推理漏洞.
我有一个具体例子的一般性问题:在拍照时,我想在Android中使用Kotlin协程魔法而不是回调地狱.
manager.openCamera(cameraId, object : CameraDevice.StateCallback() {
override fun onOpened(openedCameraDevice: CameraDevice) {
println("Camera onOpened")
// even more callbacks with openedCameraDevice.createCaptureRequest()....
}
override fun onDisconnected(cameraDevice: CameraDevice) {
println("Camera onDisconnected")
cameraDevice.close()
}
...
Run Code Online (Sandbox Code Playgroud)
我怎么把它转换成......错误......不那么难看的东西? 是否可以使用三个左右的函数进行平均回调,并通过将主流指定为promise-result路径将其转换为promise链? 如果是这样,我应该/我是否应该使用协同程序使其异步?
我喜欢async和.await会产生的东西
manager.open(cameraId).await().createCaptureRequest()
Run Code Online (Sandbox Code Playgroud)
我试图通过以下内容来做到这一点,但是...我不认为我正在使用CompletableDeferred!
suspend fun CameraManager.open(cameraId:String): CameraDevice {
val response = CompletableDeferred<CameraDevice>()
this.openCamera(cameraId, object : CameraDevice.StateCallback() {
override fun onOpened(cameraDevice: CameraDevice) {
println("camera onOpened $cameraDevice")
response.complete(cameraDevice)
}
override fun onDisconnected(cameraDevice: CameraDevice) {
response.completeExceptionally(Exception("Camera onDisconnected $cameraDevice"))
cameraDevice.close()
}
override fun onError(cameraDevice: CameraDevice, error: Int) {
response.completeExceptionally(Exception("Camera onError $cameraDevice …Run Code Online (Sandbox Code Playgroud) EnumSet,就像enum它自身一样古老(自Java 5开始),应该是比特场的用例的非妥协替代品:像位域一样快速和精简(好吧,除了不是原始类型),以及类型安全的启动.另一方面,最新和多年来最受期待的Java API-Streams API - 毫不掩饰地使用了bitfields Spliterator的特性.
我是否应该认为上述内容是核心Java专家的明确承认,EnumSet毕竟不是那么好?我是否应该重新考虑从不使用位域的常见最佳实践建议?
Streams API中缺少的一个功能是"分区依据"转换,例如Clojure中定义的.假设我想重现Hibernate的fetch join:我想发出一个SQL SELECT语句来从结果中接收这种对象:
class Family {
String surname;
List<String> members;
}
Run Code Online (Sandbox Code Playgroud)
我发出:
SELECT f.name, m.name
FROM Family f JOIN Member m on m.family_id = f.id
ORDER BY f.name
Run Code Online (Sandbox Code Playgroud)
我检索一个平坦的(f.name, m.name)记录流.现在我需要将其转换为Family对象流,并在其中包含其成员列表.假设我已经有了Stream<ResultRow>; 现在我需要将其转换为a Stream<List<ResultRow>>然后使用映射转换对其进行操作,将其转换为a Stream<Family>.
转换的语义如下:List只要提供的鉴别器函数保持返回相同的值,就保持将流收集到for中; 一旦值改变,发出List作为输出流的元素并开始收集新的List.
我希望能够编写这种代码(我已经有了这个resultStream方法):
Stream<ResultRow> dbStream = resultStream(queryBuilder.createQuery(
"SELECT f.name, m.name"
+ " FROM Family f JOIN Member m on m.family_id = f.id"
+ " …Run Code Online (Sandbox Code Playgroud) 使用channel.close()关闭kotlinx.coroutines通道的原因是什么以及不手动关闭通道的负面影响是什么?如果我不手动关闭频道会有一些不必要的处理?是否会在某个地方引用阻止其成为GCd的频道?或者,关闭功能是否仅作为向渠道的潜在用户通知其不再可以使用的方式存在.
(转自Kotlin论坛的问题https://discuss.kotlinlang.org/t/closing-coroutine-channels/2549)
在分析最近一个问题的结果时,我遇到了一个非常奇怪的现象:显然,HotSpot的额外一层JIT优化实际上会降低我机器上的执行速度.
这是我用于测量的代码:
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
@OperationsPerInvocation(Measure.ARRAY_SIZE)
@Warmup(iterations = 2, time = 1)
@Measurement(iterations = 5, time = 1)
@State(Scope.Thread)
@Threads(1)
@Fork(2)
public class Measure
{
public static final int ARRAY_SIZE = 1024;
private final int[] array = new int[ARRAY_SIZE];
@Setup public void setup() {
final Random random = new Random();
for (int i = 0; i < ARRAY_SIZE; ++i) {
final int x = random.nextInt();
array[i] = x == 0? 1 : x;
}
}
@GenerateMicroBenchmark public int normalIndex() …Run Code Online (Sandbox Code Playgroud) 在Chrome的JavaScript控制台中:
> function create(proto) {
function Created() {}
Created.prototype = proto
return new Created
}
undefined
> cc = create()
Created {}
> cc
Created {}
Run Code Online (Sandbox Code Playgroud)
Created是函数私有的create函数; create完成后,没有(我知道)引用Created.然而,Chrome可以随时显示该功能的名称,从它创建的对象开始.
Chrome没有通过遵循"天真"方法实现这一目标:
> cc.constructor
function Object() { [native code] }
> cc.toString()
"object [Object]"
Run Code Online (Sandbox Code Playgroud)
无论如何,我没有constructor把proto争论传递给create:
> cc.__proto__.hasOwnProperty("constructor")
false
Run Code Online (Sandbox Code Playgroud)
我猜测的是,Created为了instanceof机制,JavaScript VM仍然存在.有人说instanceof
测试一个对象在其原型链中是否具有构造函数的prototype属性.
但是在我输入的上面的代码中create(),有效地传递undefined了原型; 因此Created甚至没有prototype设置到实际cc.__proto__.如果我们破解create公开Created …
以下代码抛出
Exception in thread "main" java.lang.ClassCastException: test.Subclass2 cannot be cast to test.Subclass1
at test.LambdaTest.main(LambdaTest.java:17)
Run Code Online (Sandbox Code Playgroud)
public class LambdaTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ToLongFunction<B> fn1 = serde((ToLongFunction<B> & Serializable) B::value);
ToLongFunction<C> fn2 = serde((ToLongFunction<C> & Serializable) C::value);
fn1.applyAsLong(new B());
fn2.applyAsLong(new C()); // Line 17 -- exception here!
}
private static <T extends Serializable> T serde(T t) throws IOException, ClassNotFoundException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
new ObjectOutputStream(bos).writeObject(t);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos
.toByteArray()));
return (T) …Run Code Online (Sandbox Code Playgroud) java ×6
java-8 ×3
kotlin ×3
coroutine ×2
android ×1
bit-fields ×1
constructor ×1
cpu ×1
enumset ×1
intel ×1
java-stream ×1
javascript ×1
jdk1.6 ×1
jmh ×1
string ×1
volatile ×1