Sie sind auf Seite 1von 4

6/13/13

A bunch of questions on threads. (Threads forum at JavaRanch)

File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Java Threads and Synchronization

Author
Dmitry Zhuravlev Ranch Hand Joined: Apr 14, 2010 Posts: 90

A bunch of questions on threads.


posted 6/27/2011 2:53:11 PM

Gentlemen, I continue with my concurrency study and need some help with a set of concurrency issues, please. 1) Imagine we have a volatile variable which holds a reference to some object. This attributes of that variable are concurrently modified by several threads. Will all the attributes become volatile (i.e. visible to all the threads) also? Imagine we have a local variable that is passed into several Runnable constructors and then modified in each of the Runnable by corresponding thread. How can we assure that all modifications made to that variable are visible to other threads? 2) Are HttpSession attributes getters/setters thread-safe? I have googled several different approaches to this issue. Some people say yes, others say it is not guaranteed. The worse is that no one garantees on the instance of the mutex is used: does HttpSession really synchronizes on itself or not? Basing on the experience of yours, would you synchronize this piece of code?
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 .

H t t p S e s s i o ns e s s i o n=r e q u e s t . g e t S e s s i o n ( ) ; s e s s i o n . s e t A t t a r i b u t e ( A T T R I B U T E _ N A M E ,a t t r i b u t e V a l u e ) ;

3) There are several ways to deal with tasks which result is needed after delay. Is there any
www.coderanch.com/t/543184/threads/java/bunch-questions-threads 1/4

6/13/13

A bunch of questions on threads. (Threads forum at JavaRanch)

significant difference between using the countDownLatch.await(TIMEOUT, units), futures.get(TIMEOUT, units) and ExecutorService.invokeAll(tasks, TIMEOUT, units)? 4) I have a ThreadPool in my application which can be used by several threads simultaneously. It looks like

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .

M y U t i l s C l a s s { p r i v a t ef i n a ls t a t i cE x e c u t o r S e r v i c ep o o l=E x e c u t o r s . n e w F i x e d T h r e a d P o o l ( 1 0 ) ; p u b l i cs t a t i cE x e c u t o r S e r v i c eg e t P o o l ( ){ r e t u r np o o l ; } } M y M a i n C l a s s{ E x e c u t o r S e r v i c ep o o l=M y U t i l s C l a s s . g e t P o o l ( ) ; p o o l . i n v o k e A l l ( . . . ) ; }

Is ExecutorService is thread-safe? Generally how can I know whether the class from java package is thread-safe? Do I need to place a pool.shutdown() call somewhere, when my application ends? If yes, where can I place it? I cannot use finalize() because its a non-static method and there would be no instances of MyUtilsClass.
Stephan van Hulst Bartender Joined: Sep 20, 2010 Posts: 3065

posted 6/27/2011 3:47:16 PM


Dmitry Zhuravlev wrote:

1) Imagine we have a volatile variable which holds a reference to some object. This attributes of that variable are concurrently modified by several threads. Will all the attributes become volatile (i.e. visible to all the threads) also?

1
I like...

No. Volatile applies only to the variable, not any objects it may refer to.

Imagine we have a local variable that is passed into several Runnable constructors and then modified in each of the Runnable by corresponding thread. How can we assure that all modifications made to that variable are visible to other threads?

By proper use of synchronization, or by making the object you are passing thread-safe.

2) Are HttpSession attributes getters/setters thread-safe?

HttPSession is an interface, so no. There may be subclasses that are not thread-safe.

Basing on the experience of yours, would you synchronize this piece of code?

If that piece of code can be called by several threads in your program concurrently, then yes.
www.coderanch.com/t/543184/threads/java/bunch-questions-threads 2/4

6/13/13

A bunch of questions on threads. (Threads forum at JavaRanch)

3) There are several ways to deal with tasks which result is needed after delay. Is there any significant difference between using the countDownLatch.await(TIMEOUT, units), futures.get(TIMEOUT, units) and ExecutorService.invokeAll(tasks, TIMEOUT, units)?

Yes. The documentation describes these difference. Use the method that is appropriate in your context. If you have a thread that needs to wait for something to count down, use CountDownLatch. If you have several tasks that need to be executed, and you want them to cancel after the timeout, use ExecutorService. If you simply want to wait a certain amount of time for the result of a task to become available, use Future.

4) [...] Is ExecutorService is thread-safe? Generally how can I know whether the class from java package is thread-safe?

Again, ExecutorService is an interface, so no. There may be subclasses that are not thread-safe. You can tell if a specific class or interface is thread-safe if their documentation says so. If the ExecutorService interface said that all implementing classes must be thread-safe, only then may you assume it's thread safe. Usually, the last paragraph in the overview of a class will say how it behaves in regards to concurrency. Sadly, they didn't document this in the ExecutorService interface, nor in the ThreadPoolExecutor class. I would assume however, that something as an ExecutorService would be safe for use by multiple threads. This is a dangerous assumption though.

Do I need to place a pool.shutdown() call somewhere, when my application ends? If yes, where can I place it? I cannot use finalize() because its a non-static method and there would be no instances of MyUtilsC lass.

When your program ends? No, you don't have to. When your program ends, it ends, and all resources are reclaimed automatically. When you're done using an ExecutorService and your program isn't sure to end right afterwards, shut it down. Usually it's good practice to shut it down in a try-finally block, regardless.
Dmitry Zhuravlev Ranch Hand Joined: Apr 14, 2010 Posts: 90 posted 6/30/2011 12:45:29 PM

Stephan, thanks! One more question arrived. Are ServletContext attributes volatile? Imagine one user on a web-site changes ServletContext attribute via submitting some data to Servlet or Filter Controller. Do I need some extra coding to be sure that the commited modification is visible to all the users online and all threads in my application?

Stephan van Hulst Bartender

posted 6/30/2011 2:51:11 PM

What did the documentation say?

Joined: Sep 20, 2010 Posts: 3065 www.coderanch.com/t/543184/threads/java/bunch-questions-threads

3/4

6/13/13 Posts: 3065

A bunch of questions on threads. (Threads forum at JavaRanch)

1
I like...

Dmitry Zhuravlev Ranch Hand Joined: Apr 14, 2010 Posts: 90

posted 6/30/2011 5:30:36 PM

Stephan, it says nothing on this. May be thats because ServletContext is an interface. I have found a source of Apache Tomcat ApplicationContext which implements the ServletContext. Here is the link: http://www.docjar.com/html/api/org/apache/catalina/core/ApplicationContext.java.html As far as I see from that file all the attributes are stored inside ConcurrentHashMaps, so operations on them should be considered thread-safe.

Stephan van Hulst Bartender Joined: Sep 20, 2010 Posts: 3065

posted 6/30/2011 5:37:31 PM

You may not assume this. If the documentation of their class doesn't say it's thread safe, then you shouldn't treat it as such, even if its implementation may be. Even if you have a thread-safe implementation of ServletContext, as long as you're using a reference of type ServletContext and not the thread-safe subtype, you may not assume it's thread-safe; unless your code can guarantee the ServletContext reference always points to a thread-safe instance.

1
I like...

I agree. Here's the link: http://aspose.com/file-tools

subject: A bunch of questions on threads.

Similar Threads THREADS volatile and static keywords Concurrency - why does yield() have no effect in this example? How to correctly use a fixed size thread pool? A set of questions on concurrency.
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 12, 2013 23:19:09 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/543184/threads/java/bunch-questions-threads

4/4

Das könnte Ihnen auch gefallen