on 11-04-2005 5:22 PM
Hello everyone,
Does anyone know about thread-local variables? Have you ever used them with web dynpro and the j2ee engine?
Thanks
Fereidoon
Thanks Steve, Maksim
Does anyone of you have snipet of the code that can show me how to test Thread-Local?
Best regards Fereidoon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Fereidoon,
static class UsedClasses {
private static Set usedClasses = new HashSet();
private static ThreadLocal threadLocal = new ThreadLocal() {
protected synchronized Object initialValue() {
return usedClasses;
}
};
public static Set get() {
return (Set)threadLocal.get();
}
}
public static String toString(Object object, int paragraphCount) {
....
Class _class = object.getClass();
Set usedClasses = UsedClasses.get();
if(!usedClasses.contains(_class.getName())) {
usedClasses.add(_class.getName());
} else {
return "";
}
....
toString(childObject, ++paragraphCount);
....
}
Best regards, Maksim Rashchynski.
Max,
If I'd like to create an example of ThreadLocal <b>misuse</b> I cannot create better one
In effect, your code is degrading to the following one:
public class UsedClasses
{
private static Set usedClasses = new HashSet();
public static Set get()
{
return usedClasses;
}
}
Just one line, but consequences are dramatical:
public class UsedClasses
{
/*
* private static Set usedClasses = new HashSet();
*/
private static ThreadLocal threadLocal = new ThreadLocal()
{
/*
* this method may be non-synchronized
* while Java handles locks itself
*/
protected /* synchronized */ Object initialValue()
{
/* always new instance for every thread */
/* called by Java only once per thread */
return new HashSet();
}
};
public static Set get()
{
return (Set)threadLocal.get();
}
}
Valery Silaev
EPAM Systems
Thanks Steve, Do you know if SAP IDE has any support for it? Can you give some detaails on your usecase/solution?
Thanks Fereidoon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In answer to your question, I didn't really use any special tools from the SAP IDE to program with thread local. I just wrote the java code directly, and I had no real problems with it.
The reason was simple, I had a process in which I wanted to store some data in the database before I had an id for that process (it was generated in a later step). I stored the information in thread local memory and when I finally had the id I was able to store all the data together.
Cheers,
Steve
-
If you find a post useful, please help keep the community going by setting a good example and rewarding the poster with points.
Hello Fereidoon,
My last usecase regarding local thread variables was following: It was neccessary to dump complex hierarchical structure with cyclic references (object1->object2->object1->object2->...) to file or console. We iterated recursively through strucure and printed objects content in appropriate way. So, solution was to store hashcode of all objects which are already dumped to thread local set to prevent stack overflow error. We checked whether object is containing in set and in case it doesn`t - we dump it.
Best regards, Maksim Rashchynski.
I've used ThreadLocal variables in the J2EE engine before, but never in web dynpro.
-Steve
-
If you find a post useful, please help keep the community going by setting a good example and rewarding the poster with points.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
9 | |
8 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.