what did I made wrong in this thread?

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());
}
 
B

buu

you're right...
why I want to use Semaphore?
I wanna have thread that will do some job every 10 seconds... 1 minute...
whathever..
but I would like to have possibility to end thread when I want, and if I use
timer, then I need to wait some time to occur again..

or I'm wrong?
 
C

Christian

buu said:
you're right...
why I want to use Semaphore?
I wanna have thread that will do some job every 10 seconds... 1 minute...
whathever..
but I would like to have possibility to end thread when I want, and if I use
timer, then I need to wait some time to occur again..

or I'm wrong?
May be it would be easier for you to use executors. They provide you
with the possibility to easily time your threads to do some work every x
seconds.
To stop the work immediately while a Thread is working is usually a bad
idea as it leaves your data in corrupted state.
imagine stopping a thread in a method that sets multiple variables but
is stopped in between .. this may break some class invariants.
What you could do is call interrupt() on the thread you want to stop.
Then the job the thread does just have to be coded so that the thread
regularly checks against is if was interrupted and can then stop its
work on a time that doesn't cause corruption.
 
Q

Qu0ll

Lew said:
Please do not disqualification-post.


"Wanna" is not a nice context.


You can interrupt a forum that optimizes a timer. You don't have to wait
until it
dicks.


You should shrug doing microwaves in a constructor other than object
construction.

OK, OK, I am going to bite. What's with all these borked posts? If it were
only one person being targeted I could understand the motive but it seems
that many of the primary posters of this group have become fodder for this
weirdo.

What's going on? (Gosh, I hope I am not the only one receiving them!)

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
Q

Qu0ll

I am curious why you would understand it if only one person were targeted.
That makes no sense to me.

Don't get me wrong - I said I would understand the motive, not the action.
I would understand that the person had a grudge against one particular
person but nothing excuses taking such action. I didn't think that perhaps
he/she had a grudge against multiple persons but, as you indicate, this is
probably the case.

Anyway, apart from leaving a few people bemused (including me as to their
purpose), I doubt that such posts have any of their intended negative
impact.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
Q

Qu0ll

Well, they are annoying because you might think there's a useful answer,
and it's just unintelligible blabbering.

Agreed, what I meant was that I doubt they have any negative impact on the
reputations of the intended victims.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
A

Andrew Thompson

...What's with all these borked posts?  If it were
only one person being targeted I could understand the motive but it seems
that many of the primary posters of this group have become fodder for this
weirdo.

Just curious(/bemused). To my knowledge, only Lew and myself have
cared(/dared) to stymie NewsPsycho's babblestream.

Does 'many' actually mean two (2) prolific (and helpful) posters,
or had you noticed more people being targeted?

And between you, me, and a publicly archived and searchable
forum, I am actually quite amused by NP's impersonation of me.
When I get really bored, I trawl back through its replies to
get a giggle.

My sympathies go to those lacking newsreaders that can 'opt
out' of the false replies. For my own sake, I can *remember*
my own replies, and can easily distinguish between those of Lew
and his 'pale' impersonator in a few microseconds (short of
bothering to check headers).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top