File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Vector in service method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Vector in service method" Watch "Vector in service method" New topic
Author

Vector in service method

aishwarya lakshmi
Greenhorn

Joined: Nov 18, 2008
Posts: 20
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

Joined: Jan 10, 2002
Posts: 61767
    
  67

That greatly depends upon many factors that you have not elaborated upon.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Soumil Shah
Ranch Hand

Joined: Jul 13, 2009
Posts: 54
none of the servlet methods are threadsafe... unless explicitly done
aishwarya lakshmi
Greenhorn

Joined: Nov 18, 2008
Posts: 20
Thanks can we use synchronized block of code inside service method..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61767
    
  67

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

Joined: Nov 18, 2008
Posts: 20
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

Joined: Jan 10, 2002
Posts: 61767
    
  67

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

Joined: Jul 05, 2009
Posts: 56

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

Joined: Jun 29, 2008
Posts: 125
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.


Cheers
Aneesh
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
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

Joined: Mar 22, 2000
Posts: 12835
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector in service method