Why are my thread being terminated ?
Posted
by
Sephy
on Stack Overflow
See other posts from Stack Overflow
or by Sephy
Published on 2011-01-08T23:40:01Z
Indexed on
2011/01/08
23:53 UTC
Read the original article
Hit count: 131
Hi,
I'm trying to repeat calls to methods through 3 differents threads. But after I start my threads, during the next iteration of my loop, they are all terminated so nothing is executed... The code is as follows :
public static void main(String[] args) {
main = new Main();
pollingThread.start();
}
static Thread pollingThread = new Thread() {
@Override
public void run() {
while (isRunning) {
main.poll();
// test the state of the threads
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
};
public void poll() {
if (clientThread == null) {
clientThread = new Thread(new Runnable() {
@Override
public void run() {
//create some objects
}
});
clientThread.start();
}
else if (clientThread.isAlive()) {
// do some treatment
}
if (gestionnaireThread == null) {
gestionnaireThread = new Thread(new Runnable() {
@Override
public void run() {
//create some objects
};
});
gestionnaireThread.start();
}
else if (gestionnaireThread.isAlive()) {
// do some treatment
}
if (marchandThread == null) {
marchandThread = new Thread(new Runnable() {
@Override
public void run() {
// create some objects
};
});
marchandThread.start();
}
else if (marchandThread.isAlive()) {
// do some treatment
}
}
And for some reason, when I test the state of my different threads, they appear as runnable and then a the 2nd iteration, they are all terminated... What am I doing wrong? I actually have no error, but the threads are terminated and so my loop keeps looping and telling me the threads are terminated.... Thanks for any help.
© Stack Overflow or respective owner