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
Tag: multithreading
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’:
Android Socket BufferedReader readLine() not working when reading from the InputStream
I am trying to loop code inside a thread in order to check for new data form the server periodically. I am using InputStream.available() in order to check if there are any bytes to be read. If so, I open a new Thread and try to read the data using BufferedReader and then log it to the console. In onCreate:
websphere work manager exceeding max threads
I have defined a work manager with max threads = 16 and it is not Growable. However, in the logs I can see thread numbering is as high as 180+. I suspect due to this, threads are getting starved waiting for resources and throwing a JDBC ConnectionWaitTimeoutException after 180 seconds. Why are so many threads getting spawned when the max
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
How do I pass different value to each thread in ExecutorService?
Lets say I have an array num_array = [0,1,2,3,4…30] and I have a multi-threading function like this I wanted to know how I can pass num_array[0] to thread 1 and num_array[1] to thread 2 … num_array[24] to thread 25 Answer Just loop over the array and pass each runnable the element: Note that you should not assume the order of