小编Dor*_*Ari的帖子

Kotlin synchronized() 是否不锁定基本类型?

class Notification(val context: Context, title: String, message: String) {
    private val channelID = "TestMessages"

    companion object ID {
        var s_notificationID = -1
    }

    init {
        var notificationID = -1
        synchronized(s_notificationID) {
            if (++s_notificationID == 0)
                createNotificationChannel()
            notificationID = s_notificationID
        }
Run Code Online (Sandbox Code Playgroud)

以上是从两个线程同时调用的。中的断点createNotificationChannel()清楚地表明有时s_notificationID等于 1。

但是,如果我更改 synchronized(s_notificationID)synchronized(ID) 那么它似乎锁定得很好。

synchronized() 是否不锁定基本类型?如果是这样,为什么要编译?

kotlin

3
推荐指数
2
解决办法
116
查看次数

如何测试我的应用内定价以其他货币表示的情况?

我将 Google Play Billing 库用于应用内产品。

使用我的应用程序时,我总是会看到以我所在国家/地区货币显示的原始定价。价格出现在 Google Play 购买弹出窗口中,以及我使用的地方getPrice()

我想体验格式化价格在不同国家/地区的样子,以确保它在我自己的 UI 和计费 UI 中看起来都很好。而且只是为了亲眼看到自动转换是有效的。

我怎样才能做到这一点?

android in-app-billing google-play

2
推荐指数
1
解决办法
365
查看次数

为什么原始指针没有“弱指针”?还是在那里?

毫无疑问,共享指针是个好主意。但是,只要大型程序包含原始指针,我认为使用共享指针就有很大的风险。主要是,您将失去对指向包含原始指针的对象的指针的实际生命周期的控制,并且错误将发生在难以查找和调试的位置。

所以我的问题是,是否有尝试不向现代C ++添加不依赖于使用共享指针的“弱指针”?我的意思是仅当指针在程序的任何部分删除时,该指针将变为NULL。是否有理由不使用这种自制包装纸?

为了更好地解释我的意思,以下是我所做的“弱指针”。我将其命名为WatchedPtr。

#include <memory>
#include <iostream>

template <typename T>
class WatchedPtr {
public:
    // the only way to allocate new pointer
    template <typename... ARGS>
    WatchedPtr(ARGS... args) : _ptr (new T(args...)), _allocated (std::make_shared<bool>(true)) {}

    WatchedPtr(const WatchedPtr<T>& other) : _ptr (other._ptr), _allocated (other._allocated) {}

    // delete the pointer
    void del () {delete _ptr; *_allocated = false;}

    auto& operator=(const WatchedPtr<T> &other) { return *this = other; }

    bool isNull() const { return *_allocated; }

    T* operator->() const { return _ptr; } …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers c++11

0
推荐指数
1
解决办法
146
查看次数