• Post Reply Bookmark Topic Watch Topic
  • New Topic

ThreadLocal member  RSS feed

 
Denis Yuzvyk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If performance is unacceptable because of thread contention over this member, it should be made into a ThreadLocal member, such that each thread has its own TransformerFactory.

private static TransformerFactory transformerFactory;

How to made this field as ThreadLocal member?
Thx
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Denis Yuzvyk:
If performance is unacceptable because of thread contention over this member, it should be made into a ThreadLocal member, such that each thread has its own TransformerFactory.

private static TransformerFactory transformerFactory;

How to made this field as ThreadLocal member?
Thx


There are two ways to answer this question, directly and correctly:

First, directly:

To use threadlocal, create a new class that extends ThreadLocal. You need to override the initialValue() method to create a new transformer factory. You'll have to search your code to see how this member is created.

Then, to replace every place that uses the factory... make sure you get a local factory by calling the thread local get() method first.


Second, correctly:

Don't do this, unless you know what you are doing!! This makes separate copies for all of your threads. You need to make sure it can actually be done, and that it was never meant to be shared by different threads in the first place.

BTW, there is also contention issues with thread local, so if your contention is minor, it may not save anything to do this.

Henry
 
Denis Yuzvyk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand u correctly ?
//for field private static TransformerFactory transformerFactory;
//we do following
private static ThreadLocal transformerFactory = new ThreadLocal() {
protected synchronized Object initialValue() {
return TransformerFactory.newInstance();
}
};

public static TransformerFactory get() {
return (TransformerFactory) transformerFactory.get();
}

//it important to make this field ThreadLocal , i write CustomTag , and i need unique instance for each thread
[ December 11, 2004: Message edited by: Denis Yuzvyk ]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand u correctly ?


Yup... looks perfect.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!