java.util.concurrent

Happens before between threads and atomic variable Part 2

倖福魔咒の 提交于 2019-12-14 03:09:19
问题 Since answer to Happens before between threads and atomic variable was that the asserts don't hold, I need an alternate implementation. Thread1 and Thread2 each update Integers t1 and t2 respectively. Once they are updated no other updates occur to t1 or t2. The values assigned to t1 and t2 come from a common counter which gets incremented after each assignment. I need to ensure the following asserts are true; int cnt=0; ReentrantLock lock = new ReentrantLock(); volatile Integer t1=null;

Why is java.util.concurrent.FutureTask not serializable

南笙酒味 提交于 2019-12-13 19:52:32
问题 I am currently using Apache Wicket. I have some REST calls which take a few seconds each. Wicket allows ajax calls synchronously only, so I was trying to use Future and Callable. This is part of my class: public abstract class GetPrices extends AbstractAjaxTimerBehavior { private static final long serialVersionUID = 1L; private List<Future<List<Result>>> list; public GetPrices(Duration updateInterval, List<Callable> priceCalls) { super(updateInterval); list = new ArrayList<Future<List<Result>

Happens before between threads and atomic variable

你离开我真会死。 提交于 2019-12-13 16:07:32
问题 Suppose an AtomicInteger, c , is shared between two threads, thread1 and thread2. Thread1 sets (one time only) a volatile variable t1 using c.incrementAndGet() . Thread2 sets (one time only) a volatile variable t2 using c.incrementAndGet() . Once t1 and t2 are set they are not set again by any other thread. Suppose that after thread1 sets t1 , it checks the value of t2 and gets null . Is it guaranteed that t2 is subsequently set to a higher value then t1 ? (and visa versa). In other words are

Fill the dynamic data with each row columns by column Using iText in Java

此生再无相见时 提交于 2019-12-13 10:08:50
问题 Hi Every One I've a pdf report generated with iText containing a PdfPTable added to MultiColumnText, sometimes becomes so large that it will be split on more than one page, Here am added sample Code, Its working as Page -1 --------------------------------------------------------------------------------------------------------------------------------------------------+ | 1st - Emp1-NO | 2nd - Emp1-Name| 3rd - Emp1-Desig | | 4th - Emp2-NO | 5th - Emp2-Name| 6th - Emp2-Desig | | 7th - Emp3-NO |

Second thread doesn't start in producer consumer example

人盡茶涼 提交于 2019-12-13 03:57:51
问题 I'm trying to implement Producer and Consumer problem by using semaphores in java. The issue is when I start two threads (Producer and Consumer) consumer doesn't start and producer blocks itself after buffer is full. I mean it looks like there is only one thread which works in synchronous manner. Thus, as I mentioned I use 3 semaphores which are empty, full, and mutex. Here is the simplest code; Producer class; import java.util.concurrent.Semaphore; public class Producer implements Runnable {

What operations depend on the capacity of a LinkedHashMap ? Is there any concurrent version available?

丶灬走出姿态 提交于 2019-12-12 22:15:01
问题 I am using a ConcurrentHashMap as my datastructure because mutiple threads will be reading and writing to it concurrently. But I am finding that the client code will also need to iterate over it quite often . So I took a look in LinkedHashMap class which gives better iteration performance and found this section in its java doc : A linked hash map has two parameters that affect its performance: initial capacity and load factor. They are defined precisely as for HashMap. Note, however, that the

Modifying values in ConcurrentHashMap

送分小仙女□ 提交于 2019-12-12 10:04:19
问题 In ConcurrentHashMap there is concept of segmentation. What it means if two threads are trying to access ConcurrentHashMap they it gets divided in two blocks and default size of blocks is 16. Now suppose in a scenario where ConcurrentHashMap has only two elements and two different threads comes and thread1 tries to modify first value and thread2 tries to modify second value. In this case whether ConcurrentHashMap will go for segmentation? Now in a different scenario both the threads try to

Reinitialize fix delay in ScheduledExecutorService

我的未来我决定 提交于 2019-12-12 07:49:12
问题 As per my requirement, I have to execute some particular code after certain period of time. To do the same I have chose ScheduledExecutorService.scheduleWithFixedDelay(runnable, 0, 5, TimeUnit.SECONDS) and it's working fine for me. But according to my another requirement, the time mentioned in fixedDelay should be configurable at runtime. Means, currently total Delay is 5 seconds but latter if user want then can change the time into 60 seconds and in runtime fixedDelay will run after 60

Execute a continious task via ThreadPoolExecutor

試著忘記壹切 提交于 2019-12-12 05:40:15
问题 Facing the problem with the ThreadPoolExecutor in Java. How can I execute a continuous task using it? For example, I want to execute something like this: @Async void MyVoid(){ Globals.getInstance().increment(); System.out.println(Thread.currentThread().getName()+" iteration # "+ Globals.getInstance().Iterator); } I want it to run forever in 2 parallel asynchronous threads until the user sends a request to stop the ThreadPoolExecutor in the "/stop" controller. If I use this for example:

Best practice for executing large number of tasks [duplicate]

徘徊边缘 提交于 2019-12-12 04:14:34
问题 This question already has answers here : How to make ThreadPoolExecutor's submit() method block if it is saturated? (19 answers) Closed 2 years ago . Below code creates large number of runnable object in loop, even only 5 treads are there to process the tasks. Is there any way so that at any moment only 5 runnable object will be present in memory not the total number of tasks( 100000). ExecutorService executor = Executors.newFixedThreadPool(5); for (int i = 0; i < 10000; i++) { Runnable