> This does not seem to be a very high volume newsgroup. Could someone
> point me towards another resource where java newbs can get questions
> answered in a timely fashion?
> What are you saying? Java is losing mindshare? ;-> Ask away....
OK!
Sample code follows. I *just* started, this is like my second program
after hello world. I'm trying to write a command line utility that
starts, and then every once in awhile does "something", so I'm using a
thread. In the code sample below as is, the program starts, prints it's
intial message, and then stops. I figured that the main program was
terminating and it's new thread with it. So I added the line:
while (true) {}
in place of the comment "// iloop here"(*) to intentionally create an
infinite loop, so that the program would only terminate with a CTRL-C
or similar. This worked as far as the program not terminating, but I
still never see anything from the new thread. Thanks for any advice you
can give to the newb.
package nwserverlogmonitor1;
public class Main {
public static void main(String[] args) {
System.out.println("nwServerLogMonitor Started");
Thread t = new Thread();
t.start();
// iloop here
}
public void run() {
try {
while (true) {
System.out.println("I did something");
Thread.sleep(5000);
}
}
catch (InterruptedException ie) {}
}
}
(*) how clever, a double entendre. "iloop": "I Loop" or "infinte
loop". Either, both? A mystery for the ages to all but the author

Signature
Still Playing NWN1? Like retro-gaming? Try my (free, of course) PW:
http://games.groups.yahoo.com/group/LotV
Ian Shef - 03 Apr 2008 20:15 GMT
I could fix this for you, but I am not sure you want that. Instead, here
are some hints at what went wrong.
JB <jtbmax@columbus.rr.com> wrote in news:47f4f823$0$17343
$4c368faf@roadrunner.com:
<snip>
> This worked as far as the program not terminating, but I
> still never see anything from the new thread. Thanks for any advice you
[quoted text clipped - 3 lines]
>
> public class Main {
NOTE: Main extends Object (by default), not Thread. Also, to nitpick a
little, not a good choice of name.
> public static void main(String[] args) {
> System.out.println("nwServerLogMonitor Started");
> Thread t = new Thread();
NOTE: t is a Thread with an empty run() method.
> t.start();
Nice, but t has an empty run() method that immediately returns (ends the
Thread).
> // iloop here
> }
NOTE: This run() method never gets called or started.
> public void run() {
> try {
[quoted text clipped - 3 lines]
> }
> }
OUCH! The try-catch should print something useful, and/or should be inside
the infinite loop.
> catch (InterruptedException ie) {}
> }
> }
<snip>
Christian Williamson - 04 Apr 2008 08:04 GMT
>> What are you saying? Java is losing mindshare? ;-> Ask away....
>
> OK!
>
> Sample code follows. I *just* started, this is like my second program
> after hello world.
Great! Keep coding. It's good for the brain.
The code below works. I've added three things:
o indicated that Main implements Runnable,
o created a new instance of Main, and
o gave the new instance as an argument to the Thread constructor.
public class Main implements Runnable {
public static void main(String[] args) {
System.out.println("nwServerLogMonitor Started");
Main m = new Main();
Thread t = new Thread(m);
t.start();
while ( true ) {}
// iloop here
}
public void run() {
try {
while (true) {
System.out.println("I did something");
Thread.sleep(5000);
}
}
catch (InterruptedException ie) {}
}
}
> I'm trying to write a command line utility that
> starts, and then every once in awhile does "something", so I'm using a
[quoted text clipped - 35 lines]
> (*) how clever, a double entendre. "iloop": "I Loop" or "infinte
> loop". Either, both? A mystery for the ages to all but the author
Robert Larsen - 04 Apr 2008 08:29 GMT
> while ( true ) {}
This will probably make your CPU go crazy so do this instead:
while (true) {
try {
Thread.sleep(10000000000);
} catch (Exception e) {
}
}
...that way the CPU won't be held busy doing nothing.
Christian Williamson - 04 Apr 2008 09:36 GMT
>> while ( true ) {}
>
[quoted text clipped - 7 lines]
>
> ...that way the CPU won't be held busy doing nothing.
Yep, good catch.