- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
考虑以下摘自 Joshua Bloch - Effective Java,第 263 页的代码
// Broken - requires synchronization!
private static volatile int nextSerialNumber = 0;
public static int generateSerialNumber() {
return nextSerialNumber++;
}
One way to fix the generateSerialNumber method is to add the synchronized modifier to its declaration. This ensures that multiple invocations won’t be interleaved, and that each invocation will see the effects of all previous invocations. Once you’ve done that, you can and should remove the volatile modifier from nextSerialNumber. To bulletproof the method, use long instead of int, or throw an exception if nextSerialNumber is about to wrap.
同步
之后删除 volatile
,因为它是多余的。但是,它有什么害处吗? 如果我同时拥有同步和 volatile 的任何性能损失private static volatile int nextSerialNumber = 0;
public static synchronized int generateSerialNumber() {
return nextSerialNumber++;
}
最佳答案
这只是意味着 long 比 int 能容纳更多的数字。
or throw an exception if nextSerialNumber is about to wrap
暗示这里的问题是你用完了数字,你最终会溢出。您要确保不会发生这种情况。问题是,如果您处于可能的最大整数并且递增,则程序不会失败。它很高兴没有递增,但结果不再正确。
使用 long 将推迟这种可能性。抛出异常将表明它已经发生。
关于java - 如何使用 long 而不是 int 可以防止以下方法 - Effective Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4736018/
我刚刚开始了解@ngrx/store 和@ngrx.effects,并在我的Angular/Ionic 应用程序中创建了我的第一个效果。它第一次运行正常,但如果我再次将事件发送到商店(即再次单击按钮时
是否可以将所有自定义表达式保存在外部 .jsx 文件中,以便我可以从项目内部调用它们? 例如,假设我有一个我经常使用的表达式,它将人偶工具指向一个空对象的位置。我知道我可以将该表达式转换为一个可以接受
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
谁能帮我指出正确的方向,如何在位图上使用这些 Android 4.0 以来可用的 GPU 加速效果? 文档指出例如“它们必须绑定(bind)到 GL_TEXTURE_2D 纹理图像”。但最好的方法是什
我有一个带有背景图像的大 UIButton,上面有五个小的不可见 UIButton。我想在按下一个不可见的小按钮时触发大按钮的突出显示效果。我怎样才能做到这一点? 请帮忙。非常感谢! 最佳答案 为所有
function* rootSaga() { yield all(sagas.map(saga => fork(saga))); } sagaMiddleware.run(rootSaga);
您好,我在将 Li 设置为事件选项卡时遇到问题,我已经添加了悬停效果但是如何添加事件选项卡?。我使用 Angular 的 ng-show 和 ng-hide 来更改 li 中的图标。这是我的代码
我已经将 redux-saga 升级到最新的 0.15.x 版本,并且我已经在 [...effects] 方面解决了很多弃用问题,取而代之的是 all([.. .effects]), 请更新你的代码
我试图理解在组件函数中产生副作用与在没有传入依赖项数组的效果中产生副作用(因此应该在每次渲染时触发)之间的实际区别。据我观察,它们都以相同的频率运行。我意识到效果允许在适当的时间进行清理,但我只是对清
当谈到排队 jQuery Effects 和 jQuery UI Effects 时,我真的很困惑。当我这样做时 $('#div_id').effect('bounce').effect('shake
我正在制作故障文字效果,效果非常好。 现在我正在尝试创建一个书写效果,效果也非常好! 但是两者一起都有一个小错误。 当我将鼠标悬停在文本上时,毛刺效果起作用,但是当书写效果向文本添加一个字母时,这个字
相关,但不是修复:jQuery issue - # has no method 当我尝试在 jquery 中使用 effect 函数时,出现错误 Object [object Object] has
我有一个具有四个 ngrx 操作的 Angular (2) 应用程序: 开始 不被 reducer 处理(没有状态变化) ngrx Effect 调用异步任务并映射到 SUCCESS 或 ERROR
在第16项:“使const成员函数线程安全”中有一段代码如下: class Widget { public: int magicValue() const { std::lock
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
下一行中的最后一行继续生成“警告 C4552:'<=':运算符无效;预期运算符有副作用”错误,但我没有看到任何错误... void countDays(void) { int i=0; if (bir
当我在列表框中使用滚动查看器时,当我通过触摸滚动到达列表框的末尾时,我的整个窗口都在弹跳。当我使用鼠标滚轮时,此行为不会出现。我怎样才能禁用这种过度滚动/橡皮筋效果/回弹效果/弹跳效果。 我在 Win
第 3.5.4 节讨论了有效不可变对象(immutable对象),也就是说,一旦一个对象被安全且完整地构建,它的状态就不会被任何代码路径的任何代码改变。 Goetz 爵士举了一个例子: For exa
我有一部电影在后期效果中没有背景的关键颜色,但背景仍然是静止的。 我想检测在静止背景前行走的 2 个人的运动并将他们带到前面,这样我就可以在背景上创建效果。 那有可能吗?我使用女巫效果吗? 最佳答案
在基于类的组件中: componentDidMount() { axios.get('https://jsonplaceholder.typicode.com/posts').then((re
我是一名优秀的程序员,十分优秀!