cancel
Showing results for 
Search instead for 
Did you mean: 

Thread-Local

fereidoon_samii
Explorer
0 Kudos

Hello everyone,

Does anyone know about thread-local variables? Have you ever used them with web dynpro and the j2ee engine?

Thanks

Fereidoon

Accepted Solutions (0)

Answers (3)

Answers (3)

fereidoon_samii
Explorer
0 Kudos

Thanks Steve, Maksim

Does anyone of you have snipet of the code that can show me how to test Thread-Local?

Best regards Fereidoon

former_member182372
Active Contributor
0 Kudos

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.

Former Member
0 Kudos

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

http://www.NetWeaverTeam.com/

fereidoon_samii
Explorer
0 Kudos

Thanks Steve, Do you know if SAP IDE has any support for it? Can you give some detaails on your usecase/solution?

Thanks Fereidoon

Former Member
0 Kudos

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.

former_member182372
Active Contributor
0 Kudos

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.

Former Member
0 Kudos

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.