今天我实验室的敏感操作完全错了.电子显微镜上的执行器越过它的边界,在一系列事件之后,我损失了1200万美元的设备.我已将故障模块中的40K以上线路缩小到:
import java.util.*;
class A {
static Point currentPos = new Point(1,2);
static class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
new Thread() {
void f(Point p) {
synchronized(this) {}
if (p.x+1 != p.y) {
System.out.println(p.x+" "+p.y);
System.exit(1);
}
}
@Override
public void run() {
while (currentPos == null);
while (true)
f(currentPos);
}
}.start();
while (true)
currentPos = new Point(currentPos.x+1, currentPos.y+1);
} …Run Code Online (Sandbox Code Playgroud) 我今天正在查看我的代码库并发现了这个:
def optionsToArgs(options, separator='='):
kvs = [
(
"%(option)s%(separator)s%(value)s" %
{'option' : str(k), 'separator' : separator, 'value' : str(v)}
) for k, v in options.items()
]
return list(
reversed(
list(
(lambda l, t:
(lambda f:
(f((yield x)) for x in l)
)(lambda _: t)
)(kvs, '-o')
)
)
)
Run Code Online (Sandbox Code Playgroud)
它似乎采用参数的dict并将它们转换为shell命令的参数列表.它看起来像是在生成器理解中使用yield,我认为这是不可能的......?
>>> optionsToArgs({"x":1,"y":2,"z":3})
['-o', 'z=3', '-o', 'x=1', '-o', 'y=2']
Run Code Online (Sandbox Code Playgroud)
它是如何工作的?
此代码在Java 8中编译,但无法在Java 7中编译:
class Map<K,V> {
static <K,V> Map<K,V> empty() {return null;}
Map<K,V> put(K k, V v) {return null;}
V get(K k) {return null;}
}
class A {
static void f(Map<Integer,String> m){}
public static void main(String[] args) {
f(Map.empty());
}
}
Run Code Online (Sandbox Code Playgroud)
它不会推断出Map返回的具体类型Map.empty():
$ javac7 A.java
A.java:10: error: method f in class A cannot be applied to given types;
f(Map.empty());
^
required: Map<Integer,String>
found: Map<Object,Object>
reason: actual argument Map<Object,Object> cannot be converted to Map<Integer,String> by method invocation …Run Code Online (Sandbox Code Playgroud) class A {
public static void main(String[] args) {
System.out.println("\u2300");
System.out.println("\u10035");
}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过它写一条线(⌀)就好了,但十字符号没有出现,而只是打印数字5:
# javac A.java && java A
?
?5
Run Code Online (Sandbox Code Playgroud)
为什么?
我在Java中有以下简单的hello世界:
class A {
static {
System.out.println("Hello world");
}
}
Run Code Online (Sandbox Code Playgroud)
它按预期工作,但奇怪的是,它给出了一个错误,说主要方法之后不存在.
$ javac A.java && java A
Hello world
Exception in thread "main" java.lang.NoSuchMethodError: main
Run Code Online (Sandbox Code Playgroud)
为什么?我应该忽略它吗?我甚至试过制作一个名为"main"的方法,但它什么都没改变.
class A {
static {
main();
}
public static void main() {
System.out.println("Hello world");
}
}
Run Code Online (Sandbox Code Playgroud)
$ javac A.java && java A
Hello world
Exception in thread "main" java.lang.NoSuchMethodError: main
Run Code Online (Sandbox Code Playgroud) class A {
public static void main(String[] args) {
char a = '?';
System.out.println(a);
char castle = '';
System.out.println(castle);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以为颠倒的A做一个char很好,但是当我尝试使城堡char时它会得到3个编译错误.为什么?
$ javac A.java && java A
A.java:5: unclosed character literal
char castle = '';
^
A.java:5: illegal character: \57159
char castle = '';
^
A.java:5: unclosed character literal
char castle = '';
^
3 errors
Run Code Online (Sandbox Code Playgroud) 这个简单的Java代码添加2到一组long,然后打印是否2是集合的成员:
import java.util.*;
class A {
public static void main(String[] args) {
HashSet<Long> s = new HashSet<Long>();
long x = 2;
s.add(x);
System.out.println(s.contains(2));
}
}
Run Code Online (Sandbox Code Playgroud)
它应该打印,true因为2它在集合中,而是打印false.为什么?
$ javac A.java && java A
false
Run Code Online (Sandbox Code Playgroud) 我偶尔会看到一种模式init/1,即gen_server进程的功能会向自己发送一条消息,表明它应该被初始化.这样做的目的是让gen_server进程异步初始化自身,以便生成它的进程不必等待.这是一个例子:
-module(test).
-compile(export_all).
init([]) ->
gen_server:cast(self(), init),
{ok, {}}.
handle_cast(init, {}) ->
io:format("initializing~n"),
{noreply, lists:sum(lists:seq(1,10000000))};
handle_cast(m, X) when is_integer(X) ->
io:format("got m. X: ~p~n", [X]),
{noreply, X}.
b() ->
receive P -> {} end,
gen_server:cast(P, m),
b().
test() ->
B = spawn(fun test:b/0),
{ok, A} = gen_server:start_link(test,[],[]),
B ! A.
Run Code Online (Sandbox Code Playgroud)
该过程假定init在任何其他消息之前将收到消息 - 否则它将崩溃.此过程是否有可能在m消息之前获取init消息?
假设没有进程将消息发送到由此生成的随机pid list_to_pid,因为执行此操作的任何应用程序可能根本不起作用,无论此问题的答案如何.
我有这个代码生成HashSet并调用removeAll()它.我创建了一个类A,它只是一个int的包装器,它记录了equals调用的次数- 程序输出该数字.
import java.util.*;
class A {
int x;
static int equalsCalls;
A(int x) {
this.x = x;
}
@Override
public int hashCode() {
return x;
}
@Override
public boolean equals(Object o) {
equalsCalls++;
if (!(o instanceof A)) return false;
return x == ((A)o).x;
}
public static void main(String[] args) {
int setSize = Integer.parseInt(args[0]);
int listSize = Integer.parseInt(args[1]);
Set<A> s = new HashSet<A>();
for (int i = 0; i < setSize; …Run Code Online (Sandbox Code Playgroud)