看看这个简单的Java程序:
import java.lang.*;
class A {
static boolean done;
public static void main(String args[]) {
done = false;
new Thread() {
public void run() {
try {
Thread.sleep(1000); // dummy work load
} catch (Exception e) {
done = true;
}
done = true;
}
}.start();
while (!done);
System.out.println("bye");
}
}
Run Code Online (Sandbox Code Playgroud)
在一台机器上,它打印"再见"并立即退出,而在另一台机器上,它不打印任何东西并永远坐在那里.为什么?
我有以下课程:
Help on class A in module a:
class A(__builtin__.object)
| Methods defined here:
|
| any vegetable(self)
| TODO document this
|
| getHeight(self)
| uses the chicken to measure it
Run Code Online (Sandbox Code Playgroud)
通话any vegetable不起作用:
>>> a.A().any vegetable()
File "<stdin>", line 1
a.A().any vegetable()
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我该怎么打电话any vegetable?
好吧,我不敢相信我必须提供更多的证据,但这里有.
>>> dir(a.A)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'any vegetable', 'getHeight']
Run Code Online (Sandbox Code Playgroud)
这不是我的班级,所以请不要告诉我重写它.我只需要调用该方法.
我有一个服务,要解析的域名来自不受信任的来源.最近,它因内存不足而崩溃.我缩小了可能的原因,并得出结论,它必须与最近的DNS请求流量有关.但是,该服务在解析域名后不存储任何内容,因此这似乎不太可能,但我尝试使用导致其解析域名的请求向我的服务发送垃圾邮件,以防万一.它确实死于此.然后,在得出代表我代码中没有存储内存的结论之后,我将代码缩小到了这个范围:
import java.math.*;
import java.net.*;
class A {
static {
try {
for (BigInteger i = BigInteger.ZERO; i==i; i = i.add(BigInteger.ONE))
Inet4Address.getByName("a"+i+".dog");
} catch (Exception e) {throw new RuntimeException(e);}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用此行设置dnsmasq /etc/dnsmasq.conf以使分辨率更快:
address=/dog/127.0.0.1
Run Code Online (Sandbox Code Playgroud)
起初我跑这个时,它存活了几天,所以看起来这不是问题.但后来我用我用来启动服务的脚本运行它,这启用了安全管理器,它崩溃了:
$ javac A.java && java -Xmx80m -Djava.security.manager A
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
Could not find the main class: A. Program will exit.
Run Code Online (Sandbox Code Playgroud)
安全管理器使我的程序容易受到这种拒绝服务攻击.为什么?怎么解决?
$ java -version
java version "1.6.0_27"
OpenJDK Runtime Environment (IcedTea6 1.12.6) (Gentoo build 1.6.0_27-b27)
OpenJDK 64-Bit Server …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Java移植到Go。Java代码的字符变量值为'\ud83f'。当我尝试在Go中使用此值时,它不会编译:
package main
func main() {
c := '\ud83f'
println(c)
}
Run Code Online (Sandbox Code Playgroud)
$ go run a.go
# command-line-arguments
./a.go:3: invalid Unicode code point in escape sequence: 0xd83f
Run Code Online (Sandbox Code Playgroud)
为什么?我还尝试在Python中用该值创建一个字符串,它也起作用。由于某种原因,它在Go中不起作用。
为什么这个代码不会编译?
package main
const a = 1.000001
const base = 0
const b = a+base
func main() {
f(b)
}
func f(int) {}
Run Code Online (Sandbox Code Playgroud)
$ go run a.go
# command-line-arguments
./a.go:4: constant 1 truncated to integer
Run Code Online (Sandbox Code Playgroud)
这是说1被截断了?或者1不能被截断?它在谈论哪一个?
有人回答上面的代码没有编译因为b是float64.但是为什么这会编译:
package main
import "fmt"
const a = 1.000001
const b = a-0.000001
func main() {
fmt.Printf("%T %v\n",a,a)
fmt.Printf("%T %v\n",b,b)
f(b)
}
func f(int) {}
Run Code Online (Sandbox Code Playgroud)
$ go run a.go
float64 1.000001
float64 1
Run Code Online (Sandbox Code Playgroud)
?b是一个float64在这里,但它可以传递给f.
如何我不能在Haskell中创建一个16元组,这个错误意味着什么?
GHCi, version 7.6.2: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
<interactive>:2:1:
No instance for (Show
(t0,
t1,
t2,
t3,
t4,
t5,
t6,
t7,
t8,
t9,
t10,
t11,
t12,
t13,
t14,
t15))
arising from a use of `print'
Possible fix:
add an instance declaration for
(Show
(t0,
t1,
t2,
t3,
t4,
t5,
t6,
t7,
t8,
t9,
t10,
t11,
t12,
t13, …Run Code Online (Sandbox Code Playgroud) 我有来自两家公司asoft和bsoft的代码.我也无法改变.这是我的情况的简化版本,我非常确定有足够的信息来查找导致问题的原因.
bsoft提供IGang,代表一个可以与其他帮派作战的团伙.
package bsoft;
public interface IGang {
/** @return negative, 0, or positive, respectively
* if this gang is weaker than, equal to, or stronger
* than the other
*/
public int compareTo(IGang g);
public int getStrength();
public String getName();
public void attack(IGang g);
public void weaken(int amount);
}
Run Code Online (Sandbox Code Playgroud)
asoft提供GangWar,允许IGangs战斗:
package asoft;
import java.util.*;
import bsoft.*;
/** An `IGang` ordered by identity (name) */
public interface ComparableGang extends IGang, Comparable<IGang> {}
package asoft; …Run Code Online (Sandbox Code Playgroud) 我有一个函数f获取a的子列表LinkedList并将其传递给g,这也需要LinkedList:
public static <T> void g(LinkedList<T> l) {
}
public static <T> void f() {
LinkedList<T> l = new LinkedList<T>();
...
LinkedList<T> l2 = l.subList(i,j);
...
g(l2);
}
Run Code Online (Sandbox Code Playgroud)
但这不会编译,因为显然LinkedList.subList返回List而不是LinkedList.所以我必须改为:
LinkedList<T> l2 = (LinkedList<T>)l.subList(i,j);
Run Code Online (Sandbox Code Playgroud)
为什么?
Java允许你在匿名类中声明新字段,但我无法弄清楚如何从外部访问它们,即使将它们设置为公共也不会让我.
class A {
public static void main(String[] args) {
Object o = new Object() {
public int x = 0;
{
System.out.println("x: " + x++);
System.out.println("x: " + x++);
}
};
System.out.println(o.x);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个编译器错误:
$ javac A.java && java A
A.java:10: cannot find symbol
symbol : variable x
location: class java.lang.Object
System.out.println(o.x);
^
1 error
Run Code Online (Sandbox Code Playgroud)
为什么?
我试图将2d平面上的某些点表示为整数对.我希望这些点是不可变的(按值传递),但每个点都有一个唯一的标识.为了实现这一点,我创建了一个带有两个ints和a 的结构*string.这很好用:
package main
import "fmt"
func main() {
s1 := ""
s2 := ""
p := Point{1,2,&s1}
p2 := Point{1,2,&s2}
fmt.Println(p2==p) // want false
}
type Point struct{X int; Y int; id *string}
Run Code Online (Sandbox Code Playgroud)
$ go run a.go
false
Run Code Online (Sandbox Code Playgroud)
由于string实际上并没有使用任何东西(我只关心两个点是否相同),所以像这样制作唯一引用的规范解决方案似乎是使用指针struct{}代替:
package main
import "fmt"
func main() {
s1 := struct{}{}
s2 := struct{}{}
p := Point{1,2,&s1}
p2 := Point{1,2,&s2}
fmt.Println(p2==p) // want false
}
type Point struct{X int; Y int; id *struct{}} …Run Code Online (Sandbox Code Playgroud) java ×5
go ×3
types ×2
concurrency ×1
dns ×1
haskell ×1
identifier ×1
interface ×1
linked-list ×1
list ×1
oop ×1
pointers ×1
portability ×1
python ×1
set ×1
syntax ×1
tuples ×1
unicode ×1
visibility ×1
weak-typing ×1