answersLogoWhite

0


Best Answer

1.Only Use Local Variables

§The most important principle that aids in thread-safe coding is to use only local variables, not instance variables , in your Action class. Local variables are created on a stack that is assigned (by your JVM) to each request thread, so there is no need to worry about sharing them. An Action can be factored into several local methods, so long as all variables needed are passed as method parameters. This assures thread safety, as the JVM handles such variables internally using the call stack which is associated with a single Thread.

2.Conserve Resources

As a general rule, allocating scarce resources and keeping them across requests from the same user (in the user's session) can cause scalability problems. For example, if your application uses JDBC and you allocate a separate JDBC connection for every user, you are probably going to run in some scalability issues when your site suddenly shows up on Slashdot. You should strive to use pools and release resources (such as database connections) prior to forwarding

User Avatar

Wiki User

16y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you make struts action class thread safe?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you make a Thread in java?

You can define and instantiate a thread in one of two ways: • Extend the java.lang.Thread class. • Implement the Runnable interface. Ex: class MyFirstThread extends Thread { public void run() { System.out.println("Important job running in MyFirstThread"); } } class MyFirstRunnableClass implements Runnable { public void run() { System.out.println("Imp job running in MyFirstRunnableClass"); } }


Multithreading in java?

I am assuming that you want to know how to multithread in Java. 1) Write a class that implements Runnable. Put just the method run() in it. 2) Inside the run() method, put the code that you want your thread to run. 3) Instantiate the class (example: Runnable runnable = new MyRunnable();) 4) Make a new Thread (example: Thread thread = new Thread(runnable, <the name of your thread(optional)>); 5) Start the thread (example: thread.start();) 6) That's it! Your thread is now running. PS. Check the Java API for more information. Did that answer your question?


Is Servlet instance Thread safe If not how can you make thread safe?

No. The Servlet is not thread-safe by default. You can make it thread safe by implementing the SingleThreadedModel interface


Should new struts make noise?

No.


What do struts sound like?

They should make no noise.


Difference between struts 1.2 and struts 2.0?

What is the Difference Between Struts1 and Struts2Struts2 is more powerful framework as compared to struts1. The table given below describes some differences between struts1 and struts2S.R. NoStruts1Struts21Action class in struts1 extends the abstract base class. The problem with struts1 is that it uses the abstract classes rather than interfaces.An Action class implements an Action interface, along with other interfaces use optional and custom services. Struts 2 provides a base ActionSupport class that implements commonly used interfaces. Although an Action interface is notnecessary, any POJO object along with an execute signature can be used as an Struts 2 Action object.2In Struts1 Actions are dependent on the servlet API because HttpServletRequest and HttpServletResponse is passed to the execute method when an Action is invoked.Struts 2 Actions are not coupled to a container. Most often the servlet contexts are represented as simple Maps, allowing Actions to be tested in isolation. Struts 2 Actions can still access the original request and response, if required. However, other architectural elements reduce or eliminate the need to access the HttpServetRequest or HttpServletResponse directly.3Struts 1 recieves an input by creating an ActionForm object. Like the action classes, all ActionForms class must extend a ActionForm base class. Other JavaBeans classes cannot be used as ActionForms, while developers create redundant classes to receive the input. DynaBeans is the best alternative to create the conventional ActionForm classes.Struts 2 uses Action properties as input properties, eliminating the need for a second input object. Input properties may be rich object types which may have their own properties. The Action properties can be accessed from the web page via the taglibs. Struts 2 also supports the ActionForm pattern, as well as POJO form objects and POJO Actions. Rich object types, including business or domain objects, can be used as input/output objects. The ModelDriven feature simplifies taglb references to POJO input objects4Struts 1 supports separate Request Processors (lifecycles) for each module, but all the Actions in the module must share the same lifecycle.Struts 2 supports creating different lifecycles on a per Action basis via Interceptor Stacks. Custom stacks can be created and used with different Actions, as needed.5In Struts1 Actions are singletons therefore they must be thread-safe because only one instance of a class handles all the requests for that Action. The singleton strategy restricts to Struts 1 Actions and requires extra care to make the action resources thread safe or synchronized while developing an application.In Struts 2 doesn't have thread-safety issues as Action objects are instantiated for each request. A servlet container generates many throw-away objects per request, and one more object does not impose a performance penalty or impact garbage collection6Struts1 application has a major problem while testing the application because the execute method exposes the Servlet API. Struts TestCase provides a set of mock object for Struts 1.Struts 2 Actions can be tested by instantiating the Action, setting properties, and invoking methods. Dependency Injection support also makes testing simpler.7Struts1 integrates with JSTL, so it uses the JSTL EL. The EL has basic object graph traversal, but relatively weak collection and indexed property support.Struts 2 can use JSTL, but it also supports a more powerful and flexible expression language called "Object Graph Notation Language" (OGNL).8Struts 1 ActionForm properties are usually all Strings. Struts 1 uses Commons-Beanutils for type conversion. Converters are per-class, and not configurable per instance.Struts 2 uses OGNL for type conversion and converters to convert Basic and common object types and primitives as well.9Struts 1 uses manual validation that is done via a validate method on the ActionForm, or by using an extension to the Commons Validator. Classes can have different validation contexts for the same class, while chaining to validations on sub-objects is not allowed.Struts 2 allows manual validation that is done by using the validate method and the XWork Validation framework. The Xwork Validation Framework allows chaining of validations into sub-properties using the validations defined for the properties class type and the validation context10Struts 1 binds objects into the page context by using the standard JSP mechanismStruts 2 uses a ValueStack technology to make the values accessible to the taglibs without coupling the view to the object to which it is rendering. The ValueStack strategy enables us to reuse views across a range of types, having same property name but different property types.


How do you make thread in Java?

Creating a ThreadA thread in Java begins as an instance of java.lang.Thread. You'll find methods in the Thread class for managing threads including creating, starting, and pausing them. They are:start()yield()sleep()run()All the awesome action happens in the run() method. Think of the code you want to execute in a separate thread as the job to do. Lets say, you have some work that needs to be done, in the background while other things are happening in the program, so what you really want is that work to be executed in its own thread. All that code you want executed in a separate thread goes into the run() method.Ex:public void run() {// your job code goes here}The run() method will call other methods, of course, but the thread of execution-the new call stack-always begins by invoking run(). So where does the run() method go? In one of the two classes you can use to define your thread job.You can define and instantiate a thread in one of two ways:• Extend the java.lang.Thread class.• Implement the Runnable interface.You need to know about both for the exam, although I would personally recommend that you implement Runnable than extend Thread. Extending the Thread class is the easiest, but it's usually not a good OO practice.Now, you may ask me "Why? Why should I choose the Runnable over Thread?"Well, if you thought of the why before reading the preceding line, give yourself a pat on the back. Well done!Because, if you extend the Thread class what would you do if you have to extend another concrete parent class that has vital/important behavior that you need in your class? Get the point? Java does not support direct multiple inheritance and hence, once you extend the Thread class, you are done, for good. You cannot extend any other class. That is why I recommended the Runnable interface. Even if you implement the interface, you are free to extend any class you want. Convenient, right?Defining a ThreadTo define a thread, you need a place to put your run() method, and as we just discussed, you can do that by extending the Thread class or by implementing the Runnable interface. We'll look at both in this section.Extending java.lang.ThreadThe simplest way to define code to run in a separate thread is to• Extend the java.lang.Thread class.• Override the run() method.It looks like this:class MyFirstThread extends Thread {public void run() {System.out.println("Important job running in MyFirstThread");}}The limitation with this approach is that if you extend Thread, you can't extend anything else. And it's not as if you really need that inherited Thread class behavior, because in order to use a thread you'll need to instantiate one anyway.Keep in mind that you're free to overload the run() method in your Thread subclass:class MyFirstThread extends Thread {public void run() {System.out.println("Important job running in MyFirstThread");}public void run(String s) {System.out.println("String running is " + s);}}Note: The overloaded run(String s) method will be ignored by the Thread class unless you call it yourself. The Thread class expects a run() method with no arguments, and it will execute this method for you in a separate call stack after the thread has been started. With a run(String s) method, the Thread class won't call the method for you, and even if you call the method directly yourself, execution won't happen in a new thread of execution with a separate call stack. It will just happen in the same call stack as the code that you made the call from, just like any other normal method call.Implementing java.lang.RunnableImplementing the Runnable interface gives you a way to extend any class you like, but still define behavior that will be run by a separate thread. It looks like this:class MyFirstRunnableClass implements Runnable {public void run() {System.out.println("Imp job running in MyFirstRunnableClass");}}Regardless of which mechanism you choose, you've now got yourself some code that can be run by a thread of execution. So now let's take a look at instantiating your thread-capable class, and then we'll figure out how to actually get the thing running.Instantiating a ThreadRemember, every thread of execution begins as an instance of class Thread. Regardless of whether your run() method is in a Thread subclass or a Runnable implementation class, you still need a Thread object to do the work.If you extended the Thread class, instantiation is dead simple:MyFirstThread t = new MyFirstThread()If you implement Runnable, instantiation is only slightly less simple. To have code run by a separate thread, you still need a Thread instance. But rather than combining both the thread and the run() method into one class, you've split it into two classes-the Thread class for the thread-specific code and your Runnable implementation class for your job-that-should-be-run-by-a-thread code.First, you instantiate your Runnable class:MyFirstRunnableClass r = new MyFirstRunnableClass();Next, you get yourself an instance of java.lang. Thread, and you give it your job!Thread t = new Thread(r); // Pass your Runnable to the ThreadIf you create a thread using the no-arg constructor, the thread will call its own run() method when it's time to start working. That's exactly what you want when you extend Thread, but when you use Runnable, you need to tell the new thread to use your run() method rather than its own. The Runnable you pass to the Thread constructor is called the target or the target Runnable.You can pass a single Runnable instance to multiple Thread objects, so that the same Runnable becomes the target of multiple threads, as follows:public class TestMyThreads {public static void main (String [] args) {MyFirstRunnableClass r = new MyFirstRunnableClass();Thread aaa = new Thread(r);Thread bbb = new Thread(r);Thread ccc = new Thread(r);}}Giving the same target to multiple threads means that several threads of execution will be running the very same job and that same job will be done multiple times.


Does a 1988 Honda Accord have struts or shocks?

Both shocks on the front, struts on the back depending on the make it might have it both ways!


How do you make string on little alchemy?

To make thread all you have to do is mix cotton and tool together and that makes thread.


What is cotton thread used for?

cotton thread is used to sew and to make clothing


What thread is used to make denim?

Cotton thread is used in creating denim.


Are bad struts unsafe?

Yes. Bad struts effect the handling of a vehicle. In an emergency situation where you are required to make a quick maneuver, those bad struts can cause you to not be able to avoid an accident. They will also cause your tires to wear out prematurely.Yes. Bad struts effect the handling of a vehicle. In an emergency situation where you are required to make a quick maneuver, those bad struts can cause you to not be able to avoid an accident. They will also cause your tires to wear out prematurely.