• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Vector in service method

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That greatly depends upon many factors that you have not elaborated upon.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks can we use synchronized block of code inside service method..
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What's that smell? I think this tiny ad may have stepped in something.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic