How to use SimpleDataFomat in JEE code? - multithreading

SimpleDataFomat is not thread safe and it is not recommended to use Synchronize in JEE.
How to handle this in JEE ?
Thanks

If you have to stick to the SimpleDateFormatter class don't share the SimpleDateFormatter object in a member variable but just create a new one inside the method to be on the safe side. Another approach is to put it in a ThreadLocal.

Related

Should an instance of a JsonServiceClient be wrapped into a using statement?

Is it a best practice to wrap the ServiceStack's JsonServiceClient within a using statement?
var client = new JsonServiceClient();
client.Post(request);
versus
using (var client = new JsonServiceClient())
{
client.Post(request);
}
Which one is the best?
JsonServiceClient implements IDisposable so best practise would be to use it with a using statement.
However there are scenarios whereby you need to the share an instance of the JsonServiceClient across multiple requests (Such as when you use cookie based sessions, as the cookies are contained in the instances cookie container), in which case you would use the client without a using statement, but ensure that your application calls the Dispose method of the client, when it no longer requires the client.
This answer by gdoron further explains the best practise regarding classes that implement IDisposable such as the JsonServiceClient and the reasoning behind it.
As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.
The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):
I hope that helps.

Declare a ThreadLocal dictionary

I need to declare a dictionary that is global within a class, but the code using the dictionary will run as a thread, so I need to use ThreadLocal. How do I declare that?
ThreadLocal<IDictionary<string, MyClass>> is not correct.
Thanks
Try
private ThreadLocal<IDictionary<string, MyClass>> myDictionary;
Note that ThreadLocal only exists on .NET 4 (which I assume you're running considering the C# 4.0 tag), on lower .NET versions there is the [ThreadStatic] attribute with it's initialization quirks instead.

Kohana helper attribute

I have a question that keeps bothering me. Currently, I have started using Kohana 3.2 Framework. I've written a helper to handle some functionality - I have a number of methods, which are (as it should be) declared STATIC. But, all of these methods are somehow working with the database, so I need to load a model. Currently, every method has a non-static variable like this:
$comment = new Model_Comments;
$comment->addComment("abc");
OK, it seems to be working, but then I wanted to get rid of this redundancy by using class attribute to hold the instance of the model (with is class as well).
Something like this:
private static $comment; // Declaring attribute
self::$comment = new Model_Comment; // This is done within helper __constuct method
self::$comment->addComment("abc"); // And call it within the method.
But, I got failed with: Call to a member function addComment() on a non-object
Question is: is it possible to do it ? Maybe there are some other approaches ?
Sorry for a long story and, thanks in advice! :P
A static method cannot call a non-static method without operating on an instance of the class. So, what you're proposing won't work. There may be a way do accomplish something similar, but what about trying the following:
You could implement the singleton or factory pattern for your "helper" class. Then, you could create the model (as an attribute) as you instantiate/return the instance. With an actual instance of your "helper" class, you won't have to worry about the static scope issues.
In other words, you can create a helper-like class as a "normal" class in your application that, upon creation, always has the necessary model available.
I'd be happy to help further if this approach makes sense.
David

How do update the UI in a generic way from other threads/delegates?

I have created psedo code of what I would like to achieve. Inside of the SomeBLL().PerformBeginWork() threads will be created and maintained to do work. Using dot.net 4.0, what would be the best way to put this processing into an non-UI thread and still allow the assigned delegates to fire and update the UI in both a windows and web application without code modifications?
public class SomeBLL : BaseAsynWorker, IAsyncWorker
{
...makes threads and does work (if events are assigned then call them)
}
On a web page
SomeBLL sm=new SomeBLL();
sm.OnBeginWork+=ProcessUIUpdate;
sm.OnProgressUpdate+=ProcessUIUpdate;
sm.OnEndWork+=ProcessUIUpdate;
sm.OnHardError+=ProcessHardError;
SomeThreadClass.Spawn(sm.PerformBeginWork())
In a non UI process
SomeBLL sm=new SomeBLL();
sm.PerformBeginWork();
You're looking for the SynchronizationContext class.

GlobalInterfacePointer passing using AfxBeginthread

I am using a worker thread inside a CDialog class.
theApp.m_pWorkerThread = AfxBeginThread(Threadproc,this);
Now I need to pass a global Inerface poiner to the worker thread so as to access a COM object from outside
Can someone tell me the syntax to pass the GIT cookie and access it inside the thread.
Thanks
Well, I will answer this myself
This can be done in 3 ways actually. one way I will brief.
using the CComGitPtr from atlbase.h
Do like this :
CComGitPtr
GitPtrName (pointer to the initialized
interface)
Now extract the cookie from the DWORD dwCokie = GitPtrName->Detach();
Now pass the cookie as a content of struct(optional) inside the worker thread.
Once you are inside the worker thread, extract the cookie, call CoInitilize, and instantiate the reqiuired interface pointer.
as COMInterfacethatneedMarshalling pReqd.
Now pass the git cookie to the GI table and retrieve the marshalled COM object.
like:
// This is done inide the thread
// retrieve the passed cookie as dwCookie
CComGITPtr<COMInterfacethatneedMarshalling
pToGITTest(dwCookie);
COMInterfacethatneedMarshalling pReqd;
pToGITTest.CopyTo(&pReqd);
Now we have pToGITTest inside the thread, fully marshalled, go ahead and enjoy!
Thanks.

Resources