Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Producer consumer problem in webapplication using static BlockingDeque  RSS feed

 
chandan Khatri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Experts,

I am trying to use the producer consumer problem in a webapplication and my problem statement is as follows:
I have a scheduler that pulls the data from the DB and sends it to a third party system via a Rest service.

I am trying to speed up my scheduler by creating multiple consumers using a BlockingDeque and in which my Scheduler component works as Producer and I have created a thread pool of 'x' no of threads for my consumer.
My producer is adding items to a static BlockingDeque of my Consumer class and my consumer threads are waiting on this static BlockingDeque.

I know that the BlockingDeque in itself is Thread safe but I am not sure if a static BlockingDeque is thread safe or not. Also I am not sure about this design as well. Any suggestions/comments are welcome. Please feel free to point any mistake in the thought process of design or code as I am new to the multithreading in java and have a strong desire to learn from experts on this blog.




Thanks,
CK
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chandan Khatri wrote:I know that the BlockingDeque in itself is Thread safe but I am not sure if a static BlockingDeque is thread safe or not.


The question of whether an object is thread-safe or not has nothing at all to do with the variables which refer to it. So it doesn't matter whether you have static variables or instance variables referring to a BlockingDeque object.
 
chandan Khatri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
chandan Khatri wrote:I know that the BlockingDeque in itself is Thread safe but I am not sure if a static BlockingDeque is thread safe or not.


The question of whether an object is thread-safe or not has nothing at all to do with the variables which refer to it. So it doesn't matter whether you have static variables or instance variables referring to a BlockingDeque object.


Thanks Paul for your reply! Although It would be of great help if you can elaborate your point in view to the code snippet I have added. Again thanks for the reply
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, then. As far as thread safety is concerned it makes no difference whether your "sharedQueue" variable is static or not.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On another note, I was wondering why your LinkedBlockingDequeue is not using the generic type parameter?
 
chandan Khatri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not use the generic parameter type( ofcourse this is a bad practice). Do you see any other implication of not using generic parameter type?
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps then it might be a better idea to do an instanceof test before you assign the object you take from the BlockingDequeue to repItem?
 
chandan Khatri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thats correct! I will surely use the generic type for Deque. Any other suggestions? I want to thank you for your constructive suggestions.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chandan Khatri wrote:Any other suggestions?


None that I can think of. However I have a question ( perhaps I'm forgetting something very basic here and I need to read up things a bit or perhaps since you've not put details of your other methods I can't guess what's happening besides the mere production and consumption of data).

The thing I don't understand is why have you created another LinkedBlockingDeque in your public void execute() method in line 28 and why are you adding the results of your database query into this queue. And how is your execute() method being called? Or do you think I'm missing something there?


Chan.
 
chandan Khatri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:
chandan Khatri wrote:Any other suggestions?


None that I can think of. However I have a question ( perhaps I'm forgetting something very basic here and I need to read up things a bit or perhaps since you've not put details of your other methods I can't guess what's happening besides the mere production and consumption of data).


Chan.


Consumer thread makes a call to a third party to send the data and parse the response.

Chan Ag wrote:
The thing I don't understand is why have you created another LinkedBlockingDeque in your public void execute() method in line 28 and why are you adding the results of your database query into this queue. And how is your execute() method being called? Or do you think I'm missing something there?



The creation of new LinkedBlockingQueue was done by mistake. I forgot to remove this before posting the code on this blog.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!