How do you launch multiple threads from within Java EE?

I need to scale calls into Tomcat and it's been suggested to launch threads internally. Has anyone needed to do this and, if so, what solutions did they come up with?

Asked by: Sawyer648 | Posted: 21-01-2022

Answer 1

Creating your own threads inside an application server is generally discouraged because the server should manage threads for better scalability. You can also run into problems if the container makes assumptions about what's available in a thread context, such as security information (e.g., authenticated Subject). That typically happens if you spawn a thread and then use a server resource from that thread which is unknown to the container.

Check to see if there is a way to get container managed threads from Tomcat. WebLogic and WebSphere support the commonj.WorkManager, which allows you to schedule work on container managed threads. Spring can also use commonj, but I'm not sure if that support is available on Tomcat.

Answered by: Connie431 | Posted: 22-02-2022

Answer 2

You shouldn't really launch threads from within your webapp unless you have a very specific need to do so. Without more details on your problem it is hard to tell if this is the right approach to solve your problem.

You might want to take a look at Quartz, which "is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application".

Answered by: Briony911 | Posted: 22-02-2022

Answer 3

Your question is a bit vague. Tomcat itself already uses a thread pool to service HTTP requests. You can increase the number of threads through Tomcat configuration - look to the Tomcat wiki for info on this.

If you mean that in your code you want to launch threads, then I advise perusing the java.util.concurrent API introduced in Java 5. Also read "Java Concurrency in Practice", which is the text on this subject.

Answered by: Brad487 | Posted: 22-02-2022

Answer 4

What is the problem you are trying to solve with threads?

If have long running tasks you should use JMS + a full Java EE container.

If you trying to handle excess load you could consider two tomcat instances, however, if you are using http sessions you will need to investigate session replication.

If you are forced to use Tomcat consider using the Executors framework in java.util.concurrency.

Answered by: Justin497 | Posted: 22-02-2022

Answer 5

as others asked, you should give more details as to what you're trying to accomplish.

Otherwise, tomcat uses thread pools. increase the number of threads in the pool. Use a newer version of tomcat -- 6.x. Use Java 6.0_10. If needed, tune the application using a profiler and fiddle with the JVM settings, if required.

Answered by: David882 | Posted: 22-02-2022

Answer 6

The J2EE abstraction for managed multithreading is JCA. In particular, take look at the WorkManager and Work classes. See also this arcicle. Spring also provides JCA-backed work manager abstraction.

Answered by: Julian323 | Posted: 22-02-2022

Similar questions

Create multiple Java Threads at once

Is there any possibility to create the threads as follows, Thread odjhygThread= new Thread(objJob1, objJob2); I think we couldn't, if we want to create what needs to be done? anyone knows the answer? Thanks in advance.

How to handle OUT OF MEMORY error for multiple threads in a Java Web Crawler

I'm fairly new to programming and am working for my dissertation on a web crawler. I've been provided by a web crawler but i found it to be too slow since it is single threaded. It took 30 mins to crawl 1000 webpages. I tried to create multiple threads for execution and with 20 threads simultaneously running the 1000 webpages took only 2 minutes. But now I'm encountering "Heap Out of Memory" errors. I'm sure what i did was...

java - how to use multiple threads to process large number of files stored in the local disk ( using file lock)

how to use multiple threads in java to process large number of files stored in the local disk directory ( using file lock)

java - How to deal with multiple threads in one class?

Threads are often designed in two ways (see java tutorials): either by extending the Thread class or by implementing the Runnable class. Either way, you need to specify what will run inside the thread. I designed a class, an adapter towards an online resource, that retrieves different kinds of information. T...

java - Memory access by multiple threads

I'm writing a multi threading java application that runs on Nehalem processor. However I have a problem that starting from 4 threads I almost don't see the speedup in my application. I've made some simple test. I've created a thread that just allocates a big array and making access to random entries in the array. So when I run number of threads the running time shouldn't change (assuming I'm not exceeding number of...

java - How to log data from multiple threads?

There are huge numbers of threads running in parallel continuously (let's assume this continuous part)). All the threads want to log some application data, basically a set of values. What would be the best approach to log this data? single/multiple file? What would be the best approach to make backup of this log? What would be the approach to read data from ...

java - Can multiple threads wait on one object at once?

If wait can only be called from a synchronized context, and you can only call wait on an object while holding its lock, then how can multiple threads wait on the same object? Furthermore, since notify must also be called from a synchronized context, how can the notify occur?

java - Having quartz execute a job only in one thread when there are multiple quartz threads

I was wondering if one can configure quartz to execute a long processing job run only in one thread at any given time. In another words, say I have quartz configured with a SimpleThreadPool of size 5. And I have a job that fires every 10 seconds but that could take longer than 10 seconds to complete in certain situations. Is there a way to configure quartz trigger/job/scheduler so that this trigger won't fire again as it i...

java - Log4j Multiple Threads

I am adding ConsoleAppender to rootlogger for log4j as BasicConfigurator.configure(new ConsoleAppender(layout, "System.err")); But somehow log messages in some loggers down the hierarchy are not reaching console. I have not seen any instance in the hierarchy setting the additivity flag as false. Some loggers are running in different threads, do you I need to do any configuration? ...

java - Junit multiple threads

I have a test case that provides arguments and executes the main method of a class. What would be the best approach using Junit to have multiple threads concurrenlty execute the main method of class.

java - How do I combine multiple BIRT reports

We currently have a whole suite of report designs that cover various parts of our app, and these reports are generated on demand by our users. I want to be able to bundle up several of these reports into a single report to return to the user. I initially hacked up a custom report builder that generated report design files using segments inside a report library file, and then ran that generated design, but t...

java - How to split a huge zip file into multiple volumes?

When I create a zip Archive via*, is there a way to split the resulting archive in multiple volumes? Let's say my overall archive has a filesize of 24 MB and I want to split it into 3 files on a limit of 10 MB per file. Is there a zip API which has this feature? Or any other nice ways to achieve this? Thanks Thollsten

How should I cast for Java generic with multiple bounds?

Is it possible to cast an object in Java to a combined generic type? I have a method like: public static <T extends Foo & Bar> void doSomething(T object) { //do stuff } Calling this method is no problem if I have a class that implements both interfaces (Foo & Bar). The problem is when I need to call this method the object I need to pass to it is received a...

Vectors in Java, how to return multiple vectors in an object

I'm working on a java program, and I have several vectors defined and filled (from a file) inside a method. I need to return the contents of all the vectors from the method. I have heard you can put them all in one object to return them. Is that possible, and if so, how? If not, do you have any possible solutions for me? Thanks in advance for your help! Here is a code snippet: Object getInven...

java - Multiple rows per record in JSF?

having a myfaces datatable, is is possible to have 2 rows for each record? my simple one-row table looks like the following: <h:dataTable id="somelist" value="#{MyBean.somelist}" var="item"> <h:column> <f:facet name="header"> <h:outputText value="ID"/> </f:facet> <h:outputText value="#{}"/> </h:column&...

java - How to access multiple JPanels inside JFrame?

I have a JFrame that contains a "display" JPanel with JTextField and a "control" JPanel with buttons that should access the contents of the display JPanel. I think my problem is related on how to use the observer pattern, which in principle I understand. You need to place listeners and update messages, but I don't have a clue where to put these, how to get ac...

java - JPA Multiple Embedded fields

Is it possible for a JPA entity class to contain two embedded (@Embedded) fields? An example would be: @Entity public class Person { @Embedded public Address home; @Embedded public Address work; } public class Address { public String street; ... } In this case a Person can contain two Address instances - home and work. I'm us...

java - Can you require multiple types at once?

Basically I want to do this: public interface A { void a(); } public interface B { void b(); } public class SomeClass { public SomeClass(<A&B> e) { // Note the type here e.a(); e.b(); } } What I did on the commented line is obviously illegal. I know I can just require the passed object to implement interface A, or interface B, but is there a way to...

java - Is it possible to run JUnit tests from multiple packages in Eclipse?

Is it possible to run JUnit tests for multiple packages at the same time without manually creating test suites. For example if I have the hierarchy: code.branchone code.branchone.bbb code.branchtwo code.branchtwo.bbb Is it possible to: Run all tests in code.branchone and in descendent packages Run all tests in say c...

java - How can I share my JSP .tag files between multiple contexts?

I have a set of .tag files (for example a tag that renders a copyright notice) that I want to share across all my application contexts on my Tomcat application server. I've only ever used them in a context's /WEB-INF/tags directory, referring to them via the taglib directive tagdir = "/WEB-INF/tags" How can I make the tags available to all my contexts?

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)