Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

can I have a static variable in multilethread environment?

Thread view: 
www - 05 Oct 2007 18:35 GMT
Hi,

I *really* hope to have a static variable in Class A. It is a
PrintWriter.  All other classes, Class B, Class C, etc, need that
variable to print out logging message.

If not static, it is really hard to pass that variable around, one class
calling another, which calls another class. If it is static, e.g:

public Class A {
    public static PrintWriter _logger = new PrintWriter(new
FileOutputStream("mylog.txt"));
        ...

}

Then in other classes, if need to log some message, just:

A._logger.println("Here is the new message");

However, my program will be run in multilethread environment. The above
approach is not acceptable.

Is there a way to get around?

Thank you very much.
Stefan Ram - 05 Oct 2007 18:50 GMT
>Is there a way to get around?

http://download.java.net/jdk7/docs/api/java/lang/ThreadLocal.html
Matt Humphrey - 05 Oct 2007 18:51 GMT
| Hi,
|
[quoted text clipped - 20 lines]
|
| Is there a way to get around?

Although there are ways to correctly manage a static variable in a
multithreaded environment, the real question is why aren't you using a
logging package such as log4j, Java logging, etc.  These already include a
huge variety of logging styles and capabilities, work in multithreaded
environments and are remarkably easy to use.

http://logging.apache.org/log4j/1.2/index.html

Matt Humphrey http://www.iviz.com/
Patricia Shanahan - 05 Oct 2007 19:12 GMT
> Hi,
>
[quoted text clipped - 22 lines]
>
> Thank you very much.

Do all threads need the same value, or different values, and does the
value change?

If the same, non-changing value, I think a simple static variable is the
answer. The initialization is protected by the rules for class
initialization. See
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.2

If it changes, you need synchronization to manage the changes.

If different threads need different values, use ThreadLocal.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.