ReentrantLock – should we check who holds the lock while unlocking in finally block?

Advertisements A simple snippet get stuck often when delay is slightly high: public String getToken(String userId, ReentrantLock lock) throws InterruptedException { try { // lock is in a cache of ConcurrentHashMap with key of userId if (!lock.tryLock(LOCK_TIMEOUT_SEC, TimeUnit.SECONDS)) { throw new AppException("Could not get lock in " + LOCK_TIMEOUT_SEC + "s"); // 10 sec }… Read More ReentrantLock – should we check who holds the lock while unlocking in finally block?

why do we need a reference count in this Reentrant lock example?

Advertisements Why do we need m_refCount in the example below? What would happen if we leaved it out and also removed the if statement and just left its body there ? class ReentrantLock32 { std::atomic<std::size_t> m_atomic; std::int32_t m_refCount; public: ReentrantLock32() : m_atomic(0), m_refCount(0) {} void Acquire() { std::hash<std::thread::id> hasher; std::size_t tid = hasher(std::this_thread::get_id()); // if… Read More why do we need a reference count in this Reentrant lock example?

Can std::future cause coredump without get or wait

Advertisements void func() { std::future<int> fut = std::async(std::launch::async, []{ std::this_thread::sleep_for(std::chrono::seconds(10)); return 8; }); return; } Let’s say that I have such a function. An object fut of std::future<int> is initialized with a std::async job, which will return an integer in the future. But the fut will be immediately released after the function func returns. Is… Read More Can std::future cause coredump without get or wait

Unable to read/write to file from Lua script running from HAPRoxy

Advertisements I was using Lua with HAProxy to write logs into custom log file. Although my script is running totally fine. But I don’t see anything written in my text file. Here is my lua script which I am loading from HAProxy.cfg. local function foo(value) — MY CODE — file = io.open("test.lua", "a") io.output(file) io.write("The… Read More Unable to read/write to file from Lua script running from HAPRoxy

Does running a synchronous method using Task.Run() make it asynchronous

Advertisements Let’s consider a class (This is not the class or code I’m using in my application, just trying to learn with this example) class Person { public int Id {get; set;} public string Name {get; set;} public int DeptId {get; set;} public string Department {get; set;} } Now let’s assume I get a List… Read More Does running a synchronous method using Task.Run() make it asynchronous

Wait till first method is executed in all threads till execute second

Advertisements I have a small demo code where I have a Bean class and BeanRegister class. Bean class has two methods which are preInit() and postInit(). And BeanRegister is a thread class which has Bean class as a field. Here my code: public static void main(String[] args) { Bean beanA = new Bean(); BeanRegister beanRegister1… Read More Wait till first method is executed in all threads till execute second

How to remove Sonar issue on Java stream "Refactor the code so this stream pipeline is used"

Advertisements I’m working on a project (Java 17) where I have a list of object with two properties, actionId (String) and right (boolean). I’m trying to get the actionId for object with right = true and store the result as List of String. This is my code: List<String> usserValidActionsArray = userActionGatewayDTO.stream().filter(UserActionGatewayDTO::getRight) .map(UserActionGatewayDTO::getActionId).toList(); My code works… Read More How to remove Sonar issue on Java stream "Refactor the code so this stream pipeline is used"