Solved

Java always have 10 threads running in a loop

Posted on 2013-10-24
7
250 Views
Last Modified: 2013-10-24
Hi

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;

                @Override
                public void run() {
                    while (true) {

-->get data from db
-->value = value from db
-->myprocess()

} //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?


Thanks
0
Comment
Question by:netslt
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

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

Author Comment

by:netslt
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?

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39597694
Well you wouldn't add new tasks would you? Why not just have n tasks running constantly?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:netslt
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()?
0
 
LVL 86

Expert Comment

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

Author Closing Comment

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

Expert Comment

by:CEHJ
ID: 39598062
:)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
servlet example 11 49
JAVA API design with micro service cloud in mind 1 46
Java basic valueOf question 1 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

860 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