我有一个python脚本,我通过它调用
pipenv run python3 script.py
Run Code Online (Sandbox Code Playgroud)
昨天它工作正常,但今天我收到以下错误:
Traceback (most recent call last):
File "/usr/local/bin/pipenv", line 7, in <module>
from pipenv import cli
File "/usr/local/lib/python2.7/dist-packages/pipenv/__init__.py", line 17,in <module>
from .cli import cli
File "/usr/local/lib/python2.7/dist-packages/pipenv/cli.py", line 89, in <module>
if ((now.tm_mon == 10) and (now.tm_day == 30)) or ((now.tm_mon == 10) and (now.tm_day == 31)):
AttributeError: 'time.struct_time' object has no attribute 'tm_day'
Traceback (most recent call last):
File "/usr/local/bin/pipenv", line 7, in <module>
from pipenv import cli
File "/usr/local/lib/python2.7/dist-packages/pipenv/__init__.py", line 17, in <module> …Run Code Online (Sandbox Code Playgroud) 有点晚了,我有一个圣诞节特别给你。有一个圣诞老人班,里面有一个ArrayList礼物和一个Map跟踪哪些孩子已经收到了他们的礼物。孩子们模仿成线程不断地同时向圣诞老人索要礼物。为简单起见,每个孩子都收到一份(随机)礼物。
这是 Santa 类中的方法偶尔会产生一个IllegalArgumentException因为presents.size()是否定的。
public Present givePresent(Child child) {
if(gotPresent.containsKey(child) && !gotPresent.get(child)) {
synchronized(this) {
gotPresent.put(child, true);
Random random = new Random();
int randomIndex = random.nextInt(presents.size());
Present present = presents.get(randomIndex);
presents.remove(present);
return present;
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
但是,使整个方法synchronized工作得很好。我真的不明白synchronized之前显示的较小尺寸的块的问题。从我的角度来看,它仍然应该确保礼物不会多次分配给孩子,并且礼物 ArrayList 上不应该有并发写入(和读取)。你能告诉我为什么我的假设是错误的吗?
java parallel-processing multithreading synchronization thread-safety