Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Thread problem: ownership

1 view
Skip to first unread message

Stijn Verholen

unread,
Oct 28, 2001, 9:08:00 AM10/28/01
to
Hello again!
I have to write a typewriter class by myself. Found one on the internet that
works, but would like to do it in the following (simple) way:

String s = "...";
char[] arr = s.toCharArray();
long timeout = 100;
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]);
wait(timeout);
}

This would accomplish on 8 lines what the other class uses about 30 lines
for (- it is an applet and also contains parameters -).

When I compile this, I get:
non-static method wait cannot be referenced from a static context.
This is easily solved by removing static from the method definition, but the
thing is it seems to cascade all the way through to the main method, which
HAS to be static.

So I started looking and came out on threads.
Once the class was made an extension of Thread, it compiled fine.
When running however, an error is returned telling me I don't have
ownership:

java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at CharDraw.run(CharDraw.java:18)
at java.lang.Thread.run(Thread.java:484)

Does anyone know how I can give ownership of the object to the thread - or -
how I can correctly continue using my first idea?

Thanks.


Stijn Verholen

unread,
Oct 28, 2001, 11:27:58 AM10/28/01
to
P.S.: This is not a homework assignment.
I'm learning Java on my own time.

"Stijn Verholen" <sver...@easynet.be> wrote in message
news:9rh3g7$28s4$1...@rivage.news.be.easynet.net...

Marshall Spight

unread,
Oct 28, 2001, 12:54:20 PM10/28/01
to
"Stijn Verholen" <sver...@easynet.be> wrote in message news:9rh3g7$28s4$1...@rivage.news.be.easynet.net...
>
> When I compile this, I get:
> non-static method wait cannot be referenced from a static context.
> This is easily solved by removing static from the method definition, but the
> thing is it seems to cascade all the way through to the main method, which
> HAS to be static.
>

So make an instance and call a method on it:


class Foo
{
public static void main(String[] args)
{
new Foo().method();
}

void method()
{
System.out.println("This is an instance method.");
}
}


You should probably get one or more Java books and read them.


Marshall

Tor Iver Wilhelmsen

unread,
Oct 28, 2001, 12:56:44 PM10/28/01
to
"Stijn Verholen" <sver...@easynet.be> writes:

> java.lang.IllegalMonitorStateException: current thread not owner
> at java.lang.Object.wait(Native Method)
> at CharDraw.run(CharDraw.java:18)
> at java.lang.Thread.run(Thread.java:484)
>
> Does anyone know how I can give ownership of the object to the thread - or -
> how I can correctly continue using my first idea?

You need to look into the use if synchronized.

--
Tor Iver Wilhelmsen <to...@chello.no>
So the fruits of your labors have fermented into wine
And the sweat that was dripped is now the honey of the hive
- Clutch: High Caliber Consecrator

Carl Howells

unread,
Oct 28, 2001, 1:20:34 PM10/28/01
to
"Stijn Verholen" <sver...@easynet.be> wrote...

> Hello again!
> I have to write a typewriter class by myself. Found one on the internet
that
> works, but would like to do it in the following (simple) way:
>
> String s = "...";
> char[] arr = s.toCharArray();
> long timeout = 100;
> for (int i = 0; i < arr.length; i++)
> {
> System.out.print(arr[i]);
> wait(timeout);
> }
[snip]

> Does anyone know how I can give ownership of the object to the thread -
or -
> how I can correctly continue using my first idea?

The problem is that you're using wait(long). You want to be using
java.lang.Thread.sleep(long) instead. That will require you to catch
InterruptedException, but that's not a big deal.

The basic issue is that the various forms of java.lang.Object.wait() are NOT
for timed pauses. It is half of java's inter-thread semaphore mechanism,
which is complete with java.lang.Object.notify() or notifyAll(). I'd
recommend picking up the Java Threads book by O'Reilly to learn about that,
if you're interested.


Jon Skeet

unread,
Oct 29, 2001, 2:36:38 AM10/29/01
to
Stijn Verholen <sver...@easynet.be> wrote:
> Hello again!

Hello again. I notice you've asked the same question as you did before.
Any reason for ignoring the answers you were given then?

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

0 new messages