B
buu
So, I have following class, but what is strange for me is when it starts, it
executes part "System.out.println("work "+Common.getNow());" many times
until (almoust 50 times) it starts to run it every 10 seconds.....
what is wrong in this? I would like to have a thread that runs every 10
seconds, but to be available to stop it at any moment (without waiting 10
seconds to stop)
tnx a lot
public class ThreadStats extends Thread {
private static final long WAIT_TIME =10000;
private boolean work;
private Thread oTh;
private Semaphore available;
public ThreadStats() {
work=true;
available=new Semaphore(100);
try {
available.acquire();
} catch (InterruptedException e) {
// TODO
}
oTh = new Thread(this, "th");
oTh.start();
}
public void run(){
try {
System.out.println("start working");
//Thread.sleep(WAIT_TIME);
while(work){
available.tryAcquire(10000, TimeUnit.MILLISECONDS);
System.out.println("work "+Common.getNow());
}
} catch (InterruptedException e) {
// TODO
}
System.out.println("izasao "+Common.getNow());
}
executes part "System.out.println("work "+Common.getNow());" many times
until (almoust 50 times) it starts to run it every 10 seconds.....
what is wrong in this? I would like to have a thread that runs every 10
seconds, but to be available to stop it at any moment (without waiting 10
seconds to stop)
tnx a lot
public class ThreadStats extends Thread {
private static final long WAIT_TIME =10000;
private boolean work;
private Thread oTh;
private Semaphore available;
public ThreadStats() {
work=true;
available=new Semaphore(100);
try {
available.acquire();
} catch (InterruptedException e) {
// TODO
}
oTh = new Thread(this, "th");
oTh.start();
}
public void run(){
try {
System.out.println("start working");
//Thread.sleep(WAIT_TIME);
while(work){
available.tryAcquire(10000, TimeUnit.MILLISECONDS);
System.out.println("work "+Common.getNow());
}
} catch (InterruptedException e) {
// TODO
}
System.out.println("izasao "+Common.getNow());
}