Start/Stop Threads Without Killing Them

C

cppaddict

I'm looking for an elegant way to start and stop threads without
killing them, in order to avoid the overhead of unnecessary thread
creation. My awkward attempt to do this is below. I'd appreciate any
comments or suggestions about how to write code that accomplishes the
same thing more cleanly.

Thanks,
cpp

Code follows:

public class Test implements Runnable{

Thread mThread;
boolean mStopped;
public Test() {
mThread = new Thread(this);
mThread.start();
}
public synchronized void start() {
mStopped = false;
notifyAll();
}
public synchronized void stop() {
mStopped = true;
}

public synchronized void run() {
while (true) {
//give other threads a chance to start and
stop this one
try { wait(10); }
catch (InterruptedException e) {
System.out.println("Interrupted Exception!"); }
//if it does get stopped, pause here till its
restarted
if (mStopped) {
System.out.println("Stopping...");
try { wait(); }
catch (InterruptedException e) {
System.out.println("Interrupted Exception!"); }
}
System.out.println("Testing...");
}

}

public static void main (String[] args) {
Test t = new Test();
try { Thread.currentThread().sleep(1000); }
catch (InterruptedException e) {
System.out.println("Interrupted Exception!"); }
t.stop();
try { Thread.currentThread().sleep(1000); }
catch (InterruptedException e) {
System.out.println("Interrupted Exception!"); }
t.start();
try { Thread.currentThread().sleep(1000); }
catch (InterruptedException e) {
System.out.println("Interrupted Exception!"); }
t.stop();
}
}
 
D

David Hilsee

cppaddict said:
I'm looking for an elegant way to start and stop threads without
killing them, in order to avoid the overhead of unnecessary thread
creation. My awkward attempt to do this is below. I'd appreciate any
comments or suggestions about how to write code that accomplishes the
same thing more cleanly.

It sounds like you want something like a thread pool. Google turns up some
nice results for that (e.g.
http://www-106.ibm.com/developerworks/java/library/j-jtp0730.html). It's
not too hard to write a thread that executes multiple runnables in a
background thread and suspends execution whenever there are no Runnables
left. I once wrote something that looked like this (untested) code:

public class BackgroundThread {
private class TaskList implements Runnable {
private LinkedList tasks = new LinkedList();

public synchronized void addTask( Runnable r ) {
tasks.add( r );
notifyAll();
}

public synchronized Runnable removeTask() throws
InterruptedException {
if ( tasks.isEmpty() ) {
wait();
}
return (Runnable)tasks.removeFirst();
}

public void run(){
try {
while( true ) {
removeTask().run();
}
}
catch(InterruptedException e){
// Complain loudly?
}
}
}

private TaskList tasks = new TaskList();
private Thread thread = new Thread(tasks);

public void addTask(Runnable r){
tasks.addTask(r);
}

public void start() {
thread.start();
}
}
 
T

Thomas G. Marshall

cppaddict said:
I'm looking for an elegant way to start and stop threads

Let's start with an elegant way to post code examples.

There are many IDE's out there that once copied from will produce
non-indented code like yours when you paste it directly into certain
newsreaders. It seems like you are using Agent, and I'm not familiar with
their issues, but the same thing happens from Eclipse to Outlook Express,
for example.

Try pasting your code example directly into a plain vanilla text editor
first. And then copy out and paste into your newsreader.

If that doesn't work, then you might have to expand your tabs into spaces
first, but I'm not sure.


....[rip]...
 
C

cppaddict

Let's start with an elegant way to post code examples.
There are many IDE's out there that once copied from will produce
non-indented code like yours when you paste it directly into certain
newsreaders. It seems like you are using Agent, and I'm not familiar with
their issues, but the same thing happens from Eclipse to Outlook Express,
for example.

Thomas,

That code was pasted directly from plain text into Agent. It has now
been downloaded back into Agent and I still see the proper
indentation. Are you sure the problem isn't with your reader?

cpp
 
T

Tor Iver Wilhelmsen

Thomas G. Marshall said:
There are many IDE's out there that once copied from will produce
non-indented code like yours when you paste it directly into certain
newsreaders.

Looked fine to me in Emacs/Gnus.
 
C

Chris Uppal

cppaddict said:
That code was pasted directly from plain text into Agent. It has now
been downloaded back into Agent and I still see the proper
indentation. Are you sure the problem isn't with your reader?

The problem is actually Outlook Express which fails to render hard tabs in
posts.

Unfortunately many people (myself included) prefer to use OE so you (the
poster) have to decide whether to strip out tabs for our convenience or not.

-- chris
 
J

John C. Bollinger

Chris said:
cppaddict wrote:




The problem is actually Outlook Express which fails to render hard tabs in
posts.

Unfortunately many people (myself included) prefer to use OE so you (the
poster) have to decide whether to strip out tabs for our convenience or not.

So you're saying it's on the client side? I'll have to remember that.
My personal coding conventions include using only spaces for indentation
(I configure my editors so as to not automagically insert tabs when they
autoindent), but sometimes I do use tabs when I am typing code directly
into a Usenet article. I'll have to make sure not to do that.


John Bollinger
(e-mail address removed)
 
A

Andrew Thompson

...
So you're saying it's on the client side? I'll have to remember that.
My personal coding conventions include using only spaces for indentation
(I configure my editors so as to not automagically insert tabs when they
autoindent), but sometimes I do use tabs when I am typing code directly
into a Usenet article. I'll have to make sure not to do that.

I had been following this thread but resisted
posting to it till I was drunk enough(?!?)

It seems spaces are preferred to tabs for code..
is that the crux of this?

I personally *hate* tabs being replaced by spaces
in my IDE (various persons who know far more on the
matter advise me to grow up and get over it, but
that is not relevant to this thread).

To accomodate the people who are most prone to
give meaningful and relevant answers, I copy/paste
my code to a text editor and search/replace all
'tabs' for ' ' (two spaces)..

Is that what you are referring to Thomas?

I must admit, even now, I am not entirely
clear on what the problem is. Could you
be more specific and blunt? (For those of us,
like myself, who are a little slow on the uptake. )
 
T

Thomas G. Marshall

Andrew Thompson said:
I had been following this thread but resisted
posting to it till I was drunk enough(?!?)

It seems spaces are preferred to tabs for code..
is that the crux of this?

I personally *hate* tabs being replaced by spaces
in my IDE (various persons who know far more on the
matter advise me to grow up and get over it, but
that is not relevant to this thread).

To accomodate the people who are most prone to
give meaningful and relevant answers, I copy/paste
my code to a text editor and search/replace all
'tabs' for ' ' (two spaces)..

Is that what you are referring to Thomas?

I must admit, even now, I am not entirely
clear on what the problem is. Could you
be more specific and blunt? (For those of us,
like myself, who are a little slow on the uptake. )

You're not slow, this is a weird thing.

To the side of the "tab" character, even when eclipse is using spaces, if I
cut from there and paste into OE I'll get no indentation.

Odd, huh?

Both the microsoft and eclipse guys said that eclipse, and many IDE's, are
using an odd text window, and to get around that I have to first paste into
Notepad (!!!!!!!!!) and then copy and paste from /there/ into OE.

Go figger.
 
A

Andrew Thompson

Go figger.

'Go figure' software quirks? Sorry Thomas, gave
up on that about.. (checks watch) a decade ago..

I'll continue to search replace (tabs/' ') until
someones screams at me..

[ ..and would they frigin' dare, knowin' they,
what an ornery S.O.B. I can be when I put some
effort in it? ;-) ]
 
C

cppaddict

Nope, not the issue. See later post.

How is the client not the problem? The indentation appears fine in
Agent and you can see it on Google, so the tabs clearly were not
stripped. Or am I missing your point?

cpp
 
J

Juha Laiho

Thomas G. Marshall said:
There are many IDE's out there that once copied from will produce
non-indented code like yours when you paste it directly into certain
newsreaders.

It's indented just fine. The issue appears to be that your newsreader
can't handle the indentation when it's presented by TAB characters.
If that doesn't work, then you might have to expand your tabs into
spaces first, but I'm not sure.

What for? TABs are perfectly good for indentation, and don't get
corrupted by the news transit system. Fix is needed just for
newsreaders not rendering TABs properly.
 
C

Chris Uppal

Andrew said:
I personally *hate* tabs being replaced by spaces
in my IDE (various persons who know far more on the
matter advise me to grow up and get over it [...]

You should shoot them through the head; flogging's too good for 'em.

-- chris
 
C

Chris Uppal

Thomas said:
Nope, not the issue. See later post.

I have looked at your other post but I /think/ you're missing my point.

As far as I can see your other post is about the behaviour of OE when you paste
text containing hard tabs /into/ it, or press the <tab> key while editing text
in it. My experience is that OE will (at least when it's set to send plain
text) expand out the hard tabs into 4 spaces in such circumstances. It seems
to depend on the app that the text is pasted /from/ and hence the precise
format that OE "sees" in the past buffer (or whatever it's called) For
instance -- as you say -- Eclipse puts text into the buffer in a format that OE
won't render properly, I suspect that is the same "rich text" format that, say,
wordpad (not notepad) uses.

But that isn't the point I was making: "cppaddict"s post (at least as it
reached me) has hard tabs in the on-the-wire text itself, so we are talking
about OE's behaviour when it is functioning purely as a news /reader/ (of plain
text). For a check, if you "view source" of cppaddict's original post from OE
(the option is well hidden, for some reason) or drag the post to the desktop
and open the resulting .nws file with a real text editor then you should see
the tabs that OE (in reader mode) has hidden from you; if not then there's
something /very/ strange about the route your news takes on its way to you...

[Crappy software of course, and if and when somebody -- maybe me -- comes up
with a news reader that isn't
awkward/over-complicated/optimised-for-downloading-
GBytes-of-porn/ugly-as-sin/ or whatever, then I'll immediately switch to it.]

-- chris
 
T

Thomas G. Marshall

Chris Uppal said:
I have looked at your other post but I /think/ you're missing my
point.

As far as I can see your other post is about the behaviour of OE when
you paste text containing hard tabs /into/ it, or press the <tab> key
while editing text in it.

NO. If I have an eclipse example, even with spaces for tabs, it used to be
the case that I could not copy from it into OE. I'd have to first copy it
into notepad.

If what I was talking about was simply a tabs issue, then the tabs would go
into notepad, then out of notepad and into OE and then get stripped out
anyway. I'm talking about SPACE indents needing to be copied into notepad.

By the way, using wordpad (not word), will cause the same problem. The
following was typed into wordpad:

this is at 0
(4 spaces)this is at 4 spaces indent
(8 spaces)this is at 8 spaces indent

and when I cut and paste it here into OE

this is at 0
this is at 4 spaces indent
this is at 8 spaces indent

http://groups.google.com/groups?hl=...1182%24AO6.1157%40nwrdny02.gnilink.net&rnum=3

It turns out though, you're right, that this /is/ infact a different issue
than what's going on here. Sorry. The OP's message source /does/ have real
tabs in it.

Sorry for confusing the issue. I would have sworn it was the same problem.

....[rip]...
 
T

Thomas G. Marshall

cppaddict said:
How is the client not the problem? The indentation appears fine in
Agent and you can see it on Google, so the tabs clearly were not
stripped. Or am I missing your point?

You're not missing my point. My point is missing my point. I @$%ed up.
The problem I'm talking about, while similar, is not this problem at all.

Put spaced indents into WordPad or Eclipse, copy to clipboard, paste into
OE. >poof!< no indents.
 
T

Thomas G. Marshall

Juha Laiho said:
"Thomas G. Marshall"


It's indented just fine. The issue appears to be that your newsreader
can't handle the indentation when it's presented by TAB characters.


What for? TABs are perfectly good for indentation, and don't get
corrupted by the news transit system. Fix is needed just for
newsreaders not rendering TABs properly.

And how many spaces should the tabs be set at?

I know folks that demand 4 and others that demand 8. That's why I suggest
folks forget the tab character and insert spaces /anyway/, forget about OE.
 
M

Michael Borgwardt

Juha said:
What for? TABs are perfectly good for indentation, and don't get
corrupted by the news transit system. Fix is needed just for
newsreaders not rendering TABs properly.

No, Tabs are horrible for indentation. People use different widths for
them and once they become mixed with spaces (which invariably happens)
the indentation is completely fucked up for anyone with a different
tab width setting.
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top