Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector in service method

 
aishwarya lakshmi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we use vector in service method , Then the value of vector can be changed by other thread of request or not?

Please answer this question.....

Thanks
Laksh

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That greatly depends upon many factors that you have not elaborated upon.
 
Soumil Shah
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
none of the servlet methods are threadsafe... unless explicitly done
 
aishwarya lakshmi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks can we use synchronized block of code inside service method..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aishwarya lakshmi wrote:Thanks can we use synchronized block of code inside service method..

Why would you need to do that?

How are you storing the reference to the vector?
 
aishwarya lakshmi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got this question when in an interview..

The exact question is

1) can we create synchronized block of code inside service method to protect that block of code shared by multiple thread.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aishwarya lakshmi wrote:1) can we create synchronized block of code inside service method to protect that block of code shared by multiple thread.

The question is flawed. First, you need to determine if you need to in the first place.
 
Charles 'King
Ranch Hand
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aishwarya lakshmi wrote:i got this question when in an interview..

The exact question is

1) can we create synchronized block of code inside service method to protect that block of code shared by multiple thread.


The answer is yes:

 
Aneesh Vijendran
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically it can be done, but logically wrong.

If you use a vector, anyhow it is synchronized and it should do no damage. Also depends on how you are implementing.
 
swapnl patil
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,Aneesh is right . Vector is already threadsafe , so we dont need to take care for that.

and use synchronized clock only for critical code like bank application withdraw & deposit method.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Bear - the question is seriously flawed.

1. ANY number of Threads can be executing the same block of code "at the same time" without a problem. It is the variables that you have to think about, not the code. This is true in any language, not just Java.

2. ONLY the request Thread which calls service can modify the request and response variables - no other request Thread can see them.

3. ONLY the request Thread can modify variables created and reference locally inside the service method. NOTE the important word "locally" - if the only reference to an object exists in the stack of the calling Thread, it is completely isolated.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic