Skip to content
Advertisement

Tag: multithreading

Why is my StringBuffer variable not synchronized. Where as StringBuilder variable behaves as synchronized

I am trying to understand the difference between StringBuilder and StringBuffer. The goal of the below program is that 2 threads (Jack and Jill) compete to mutate a StringBuffer and StringBuilder values. If the original value is already modified then the thread will not modify that variable. Why is my StringBuffer variable not synchronized. Where as StringBuilder variable behaves as

Java mmap MappedByteBuffer

Let’s say I’ve mapped a memory region [0, 1000] and now I have MappedByteBuffer. Can I read and write to this buffer from multiple threads at the same time without locking, assuming that each thread accesses different part of the buffer for exp. T1 [0, 500), T2 [500, 1000]? If the above is true, is it possible to determine whether

Stop ExecutorService on thread failure and exception handling

This is a simplified example I did to expose my problem. I have some task doSomeWork() that I handle in a multihreading fashion using ExecutorService (4 threads at a time max). However, if any of the threads/tasks generates an exception, I would like to: Stop any further tasks from being processed. Catch the exception at the main thread level. Currently,

Is this the correct way to extract counts from a Concurrent Hash Map without missing some or double counting?

Working on something where I’m trying to count the number of times something is happening. Instead of spamming the database with millions of calls, I’m trying to sum the updates in-memory and then dumping the results into the database once per second (so like turning 10 +1s into a single +10) I’ve noticed some strange inconsistency with the counts (like

Spring boot JPA repository passed to another thread not working

I have an autowired jpa repository object working. However, I need to use it to add rows into the database from multiple threads. Though, after passing it to another thread, it fails. Code structure However, doing a dbRepository.save() from a thread safe class, I get error cause: java.lang.IllegalStateException: org.springframework.context.annotation.AnnotationConfigApplicationContext@41330d4f has been closed already detailedMessage: Error creating bean with name ‘spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties’:

JSch SFTP connections getting stuck

We have a java 7 code base where we are using Apache commons vfs2 v2.2 which is using JSch-0.1.54 as sftp provider. Now, the use case is to transfer files to a remote host via sftp. But, every now and then, the file upload process gets stuck. After taking the thread dump of the application, we found both the threads

Why is volatile keyword not needed for inter-thread visibility when one of the threads involved is the main() thread?

Consider the following program: Here the stopRequested is not declared as volatile – so ideally the thread backgroupdThread must not stop – and execute endlessly But when running this locally – the thread backgroundThread is gracefully shutting down with the message: “Stopping the thread!!”. Are all the updates by the main() thread to the shared variable stopRequested visible to the

Advertisement