• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Producer Consumer using blocking queues - Time taken with static and non static ConcurrentHashMap

 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a Fibonacci class that has a ConcurrentHashMap<Integer, Integer>. This class has a public int calculate(int x){ } method
that calculates the Fibonacci value for x recursively ( calculate(x) = calculate(x-1) + calculate(x-2) ) and returns it.
The class has the mechanism to put the computed entry in a ConcurrentHashMap ( non static ) only if the key is not already present in the map.
The recursive algorithm takes benefit of such cached entries in the ConcurrentHashMap to compute the next Fibonacci value.

I have a Producer application that has a BlockingQueue<Integer> queue and a Thread type.
The constructor of the producer takes the bounded blocking queue as its constructor argument and starts its own thread.
The producer application thread sleeps for a fixed interval and then puts a series of consecutive integer values in the bounded blocking queue.
The producer I have puts Integers 0-44 in the blocking queue, one each after sleeping for one second.

I have a Consumer application that has a BlockingQueue<Integer> queue, a Thread type, and a Fibonacci type ( Remember the Fibonacci class has a ConcurrentHashMap) .
The constructor of the consumer takes the bounded blocking queue as its constructor argument and starts its own thread.
The consumer application takes the integer from the blocking queue and computes and returns the Fibonacci for this integer.

The producers and consumers share the blocking queue.

The code for the above classes is taken from book, Java Threads - Edition 3.

When I test my application for one producer that produces integers from 0 to 44, and 4 consumers, I am getting unexpected ( I think so) results. My application runs faster if the ConcurrentHashMap in the Fibonacci class is not static. Shouldn't it be faster if the ConcurrentHashMap is static because all the consumer threads would share the ConcurrentHashMap and a put for one value will be done only once for all the Consumer Threads, if that value is absent that is. However if four consumer threads have their own copy of the ConcurrentHashMap, put operations are done more frequently.

Following are the output cases I have.

Case 1 - 1 producer, 4 Consumers, ArrayBlockingQueue<Integer>(10), Static ConcurrentHashMap in the Fibonacci class
Output
run:
Produced request 0
Thread-1 Calculated result of 0 from 0
Produced request 1
Thread-2 Calculated result of 1 from 1
Produced request 2
Thread-3 Calculated result of 1 from 2
Produced request 3
Thread-4 Calculated result of 2 from 3
Produced request 4
Thread-1 Calculated result of 3 from 4
Produced request 5
Thread-2 Calculated result of 5 from 5
Produced request 6
Thread-3 Calculated result of 8 from 6
Produced request 7
Thread-4 Calculated result of 13 from 7
Produced request 8
Thread-1 Calculated result of 21 from 8
Produced request 9
Thread-2 Calculated result of 34 from 9
Produced request 10
Thread-3 Calculated result of 55 from 10
Produced request 11
Thread-4 Calculated result of 89 from 11
Produced request 12
Thread-1 Calculated result of 144 from 12
Produced request 13
Thread-2 Calculated result of 233 from 13
Produced request 14
Thread-3 Calculated result of 377 from 14
Produced request 15
Thread-4 Calculated result of 610 from 15
Produced request 16
Thread-1 Calculated result of 987 from 16
Produced request 17
Thread-2 Calculated result of 1597 from 17
Produced request 18
Thread-3 Calculated result of 2584 from 18
Produced request 19
Thread-4 Calculated result of 4181 from 19
Produced request 20
Thread-1 Calculated result of 6765 from 20
Produced request 21
Thread-2 Calculated result of 10946 from 21
Produced request 22
Thread-3 Calculated result of 17711 from 22
Produced request 23
Thread-4 Calculated result of 28657 from 23
Produced request 24
Thread-1 Calculated result of 46368 from 24
Produced request 25
Thread-2 Calculated result of 75025 from 25
Produced request 26
Thread-3 Calculated result of 121393 from 26
Produced request 27
Thread-4 Calculated result of 196418 from 27
Produced request 28
Thread-1 Calculated result of 317811 from 28
Produced request 29
Thread-2 Calculated result of 514229 from 29
Produced request 30
Thread-3 Calculated result of 832040 from 30
Produced request 31
Thread-4 Calculated result of 1346269 from 31
Produced request 32
Thread-1 Calculated result of 2178309 from 32
Produced request 33
Thread-2 Calculated result of 3524578 from 33
Produced request 34
Thread-3 Calculated result of 5702887 from 34
Produced request 35
Thread-4 Calculated result of 9227465 from 35
Produced request 36
Thread-1 Calculated result of 14930352 from 36
Produced request 37
Thread-2 Calculated result of 24157817 from 37
Produced request 38
Thread-3 Calculated result of 39088169 from 38
Produced request 39
Thread-4 Calculated result of 63245986 from 39
Produced request 40
Thread-1 Calculated result of 102334155 from 40
Produced request 41
Produced request 42
Thread-2 Calculated result of 165580141 from 41
Produced request 43
Thread-3 Calculated result of 267914296 from 42
Produced request 44
Produced request 999
Produced request 999
Produced request 999
Produced request 999
Thread-4 Calculated result of 433494437 from 43
Thread-1 Calculated result of 701408733 from 44
BUILD SUCCESSFUL (total time: 51 seconds)

Case 2 - 1 producer, 4 Consumers, ArrayBlockingQueue<Integer>(10), Non Static ConcurrentHashMap in the Fibonacci class
Output
run:
Produced request 0
Thread-1 Calculated result of 0 from 0
Produced request 1
Thread-2 Calculated result of 1 from 1
Produced request 2
Thread-3 Calculated result of 1 from 2
Produced request 3
Thread-4 Calculated result of 2 from 3
Produced request 4
Thread-1 Calculated result of 3 from 4
Produced request 5
Thread-2 Calculated result of 5 from 5
Produced request 6
Thread-3 Calculated result of 8 from 6
Produced request 7
Thread-4 Calculated result of 13 from 7
Produced request 8
Thread-1 Calculated result of 21 from 8
Produced request 9
Thread-2 Calculated result of 34 from 9
Produced request 10
Thread-3 Calculated result of 55 from 10
Produced request 11
Thread-4 Calculated result of 89 from 11
Produced request 12
Thread-1 Calculated result of 144 from 12
Produced request 13
Thread-2 Calculated result of 233 from 13
Produced request 14
Thread-3 Calculated result of 377 from 14
Produced request 15
Thread-4 Calculated result of 610 from 15
Produced request 16
Thread-1 Calculated result of 987 from 16
Produced request 17
Thread-2 Calculated result of 1597 from 17
Produced request 18
Thread-3 Calculated result of 2584 from 18
Produced request 19
Thread-4 Calculated result of 4181 from 19
Produced request 20
Thread-1 Calculated result of 6765 from 20
Produced request 21
Thread-2 Calculated result of 10946 from 21
Produced request 22
Thread-3 Calculated result of 17711 from 22
Produced request 23
Thread-4 Calculated result of 28657 from 23
Produced request 24
Thread-1 Calculated result of 46368 from 24
Produced request 25
Thread-2 Calculated result of 75025 from 25
Produced request 26
Thread-3 Calculated result of 121393 from 26
Produced request 27
Thread-4 Calculated result of 196418 from 27
Produced request 28
Thread-1 Calculated result of 317811 from 28
Produced request 29
Thread-2 Calculated result of 514229 from 29
Produced request 30
Thread-3 Calculated result of 832040 from 30
Produced request 31
Thread-4 Calculated result of 1346269 from 31
Produced request 32
Thread-1 Calculated result of 2178309 from 32
Produced request 33
Thread-2 Calculated result of 3524578 from 33
Produced request 34
Thread-3 Calculated result of 5702887 from 34
Produced request 35
Thread-4 Calculated result of 9227465 from 35
Produced request 36
Thread-1 Calculated result of 14930352 from 36
Produced request 37
Thread-2 Calculated result of 24157817 from 37
Produced request 38
Thread-3 Calculated result of 39088169 from 38
Produced request 39
Thread-4 Calculated result of 63245986 from 39
Produced request 40
Thread-1 Calculated result of 102334155 from 40
Produced request 41
Produced request 42
Thread-2 Calculated result of 165580141 from 41
Produced request 43
Thread-3 Calculated result of 267914296 from 42
Produced request 44
Produced request 999
Produced request 999
Produced request 999
Produced request 999
Thread-4 Calculated result of 433494437 from 43
Thread-1 Calculated result of 701408733 from 44
BUILD SUCCESSFUL (total time: 49 seconds)

I know several OS processes might affect the output and hence I tested this multiple times. Almost every time I got better performance when the ConcurrentHashMap was not static. Could you help me understand why?

Thanks,
Chan.



 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please ignore this question. It is resolved. Sorry about the confusion.

Thanks,
Chan.

 
reply
    Bookmark Topic Watch Topic
  • New Topic