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

Help with Thread Sleep Calls causing lack of SLEEP

0 views
Skip to first unread message

Andrew Booth

unread,
Nov 18, 2003, 8:15:29 AM11/18/03
to
Hello

I am trying to introduce the sleep() command into my java code.

The existing program runs without the sleep command but I have
indeterminancy
I therefore want to send my threads to sleep for different lengths of time.

I have used the following

public void run()
{
for (index1 = START; index1<END; index1++)
{
try
{
sleep(3000);
}
catch(InterruptedException e){}
buf.insert(Pots[index1]);
}
}


My problem is that I can get it to compile ok but when I execute it throws a
whole host of errors.

Can anyone help?

What am I doing wrong?


The only outcome of my current sleep() call is lack of sleep on my part


TIA


Andrew


Jonas Kongslund

unread,
Nov 18, 2003, 9:24:39 AM11/18/03
to
Andrew Booth wrote:

> Hello
>
> I am trying to introduce the sleep() command into my java code.
>
> The existing program runs without the sleep command but I have
> indeterminancy
> I therefore want to send my threads to sleep for different lengths of
> time.

Are you playing around with threads and monitors? Then you probably want to
use wait instead of sleep.

> My problem is that I can get it to compile ok but when I execute it throws
> a whole host of errors.

Which errors?

Please be more specific about what program you are trying to make, i.e. what
is its specification, and what you have created so far, i.e. what is the
current implementation.

--
Jonas Kongslund

Alex Hunsley

unread,
Nov 18, 2003, 11:20:31 AM11/18/03
to
Andrew Booth wrote:
> Hello
>
> I am trying to introduce the sleep() command into my java code.
>
> The existing program runs without the sleep command but I have
> indeterminancy
> I therefore want to send my threads to sleep for different lengths of time.
>
> I have used the following
>
> public void run()
> {
> for (index1 = START; index1<END; index1++)
> {
> try
> {
> sleep(3000);
> }
> catch(InterruptedException e){}
> buf.insert(Pots[index1]);
> }
> }
>
>
> My problem is that I can get it to compile ok but when I execute it throws a
> whole host of errors.
>
> Can anyone help?
>
> What am I doing wrong?

Are you sure you don't mean Thread.currentThread().sleep(3000)?

Anyway, it sounds like you're using sleep for an evil purpose - i.e.
waiting a random set amount of time for something else to finish - you
should probably be using monitors or locking or something similar in
this case.

alex

Andrew Booth

unread,
Nov 18, 2003, 12:35:20 PM11/18/03
to
The program is for a university assignment that gives an example of the
producer - consumer problem

basically I have 2 producers and 1 consumer

I want to send producer 1 to sleep for 1 second and prooducer 2 for 2
seconds (looping each 10 times)
the consumer I want to operate at a rate of 1 every 3 secs hence sending
this thread to sleep for 3 seconds. (looping 20 times)

The program is enclosed in a single class file.

I am extending the thread class for each thread so I don't need the
Thread.sleep() command just the sleep()

The errors it throws up are as follows:


java.lang.NoClassDefFoundError: Potters2 (wrong name: Comp224/Potters2)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
Exception in thread "main"


hope this makes it clearer to some one??


Regards

Andrew


"Jonas Kongslund" <do...@mail.me.at.all> wrote in message
news:HYpub.6208$vJ7....@news.get2net.dk...

Andrew Booth

unread,
Nov 18, 2003, 12:35:20 PM11/18/03
to


Regards

Andrew

Andrew Booth

unread,
Nov 18, 2003, 12:39:10 PM11/18/03
to


Regards

Andrew

Jonas Kongslund

unread,
Nov 18, 2003, 1:27:06 PM11/18/03
to
Andrew Booth wrote:
[snip]

> The errors it throws up are as follows:
>
> java.lang.NoClassDefFoundError: Potters2 (wrong name: Comp224/Potters2)
> at java.lang.ClassLoader.defineClass0(Native Method)
> at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
> at
[snip]

I suggest that you check your classpath. The error above has nothing to do
with you invoking the sleep method on a thread.

--
Jonas Kongslund

Jon Skeet

unread,
Nov 19, 2003, 8:07:47 AM11/19/03
to
Alex Hunsley <la...@tardis.ed.ac.molar.uk> wrote:
> Are you sure you don't mean Thread.currentThread().sleep(3000)?

He certainly doesn't. sleep() is a static method. It's unfortunate that
Java allows static members to be accessed via references, but the above
does nothing different from Thread.sleep() - the current thread is
*always* the one which is set to sleep.



> Anyway, it sounds like you're using sleep for an evil purpose - i.e.
> waiting a random set amount of time for something else to finish - you
> should probably be using monitors or locking or something similar in
> this case.

Agreed.

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

0 new messages