Java always have 10 threads running in a loop

Posted on 2013-10-24
Medium Priority
Last Modified: 2013-10-24

I have a loop that basically runs indefinitely until I stop it- it is implemented with a thread because I want to be able to stop it with a button.

Inside the loop I get a value from the db, pass that value to a function that processes the value.

Then next loop is started.

Because I want to process data fast I was running 10 or 20 instances of the same application.

Now I think it would be easier to start multiple (for example 10) threads in one instance that call the processing function for "value".

How do I do this best? So that always when one thread ends a next one is created that gets a value from the db and sends it to myprocess()

This is the basic structure of the code:


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {   

        if (a == null) {
            a = new Thread(new Runnable() {
                int counters;

                public void run() {
                    while (true) {

-->get data from db
-->value = value from db

} //next

    public static void myprocess() {
do processing for the value

Open in new window

Maybe also a bit of a problem that value is a defined in a variable and not passed to the function, so maybe this gives some mixup when multithreading?

public class NewJFrame2 extends javax.swing.JFrame {
    public static String value

Open in new window

When one of the 10 threads is finished a new one should be started.

Or do I have to restructure the whole thing so it will work?

Question by:netslt
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 86

Accepted Solution

CEHJ earned 2000 total points
ID: 39597625
Sounds like you'd be better off with a thread pool with those ten threads running constantly

Author Comment

ID: 39597677
OK I now had a quick look at fixed thread pool.

What I don't understand is how  I add new tasks to the pool?

Do I do that from inside my loop?

How to make sure not to add to many tasks to the pool to fast?

What would be the basic structure to achieve the same thing as in my code example above?

LVL 86

Expert Comment

ID: 39597694
Well you wouldn't add new tasks would you? Why not just have n tasks running constantly?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 39597750
ok so that would mean that each thread starts/contains a indefinite loop that connects to the db & sends the result to myprocess()?
LVL 86

Expert Comment

ID: 39597769
Yes, that's one way of doing it. That way, you don't have the overhead of thread setup

Author Closing Comment

ID: 39597795
ok, thanks. I will try that.
LVL 86

Expert Comment

ID: 39598062

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question