Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java Bug? -> Java threads: Wait X Notify X [Thread AGAINST Runnable]

Reply
Thread Tools

java Bug? -> Java threads: Wait X Notify X [Thread AGAINST Runnable]

 
 
Renato Battaglia
Guest
Posts: n/a
 
      10-23-2003
// JAVA THREADS - A JAVA BUG? Hope not!PLEASE ANYBODY HELP ME! Read on, folks...

// Name this "Reader.java"... copy-paste it into your programm editor!

// Example of a command line that did well on my fast Pentium-Linux:
// java Reader . 1500000
// YES I KNOW it will eat your CPU cycles, but running is brief!
// PS: On my sloooow Windows machine, i use "c:\ 1000" instead of ". 1500000"!

/*
Code below (just after my cry for help) is MEANT to be buggy.

I mean... "Watcher" threads will try to SYNCH(lock) then WAIT on
"Calculator" when, actually, "Calculator" already has a lock.

"Calculator" is meant to issue a "notify" when there are still no
"Watcher" waiting on it...

So... as long as "Calculator" will never again issue a "notify", "Watcher"
threads are supposed to remain blocked, "waiting" for a never-happening event.

This is exactly what happens when "class Calculator implements Runnable".

But... oddly enough, whenever I make a simple change in "Class Calculator"
code, just making it "extends Thread" instead of "implements Runnable", madness
comes by!!!!!!!!!!!!!!!!

Behavior changes, and , immediately after method "Calculator.run()"
returns, the "c.wait()" code in "Watcher.run()" also returns!! ISNīT THAT
WRONG?
The "c.wait()" call should block forever, shouldnīt it? Boy...
I am by no means a Java Guru... and, in my first contacts with Java threading,
I face such strange stuff. Blessing or a curse?

Anyway... I canīt express myself much better than this. Hope you,
experienced Java folks, have some fun on cracking this (bug?) out.

PS: I am really eagger to receive some replies showing me how wrong
I am, and what I am doing wrong, because I fell hopelessly lost on this matter,
right now.

http://www.velocityreviews.com/forums/(E-Mail Removed)
(+55) 11 55082813
(+55) 11 55082817
*/


import java.io.*;
public class Reader
{
static String delayDirectory;
static int delayIterations;

public static void main(String argv[]) throws Exception
{
if (argv.length != 2)
{
System.err.println("Usage: java Reader [directory] [iterations]");
return;
}
delayDirectory = argv[0];
delayIterations = Integer.parseInt(argv[1]);
// please, adjust ITERATIONS and DIRECTORY to make delay
//(in Calculator.run()" take a few second... 4 or 5 seconds would
// be great for the purpose of this code-sample.
// PS: Donīt put a "sleep" there... "sleep" would release the
// object-lock, altering the behavior.


Calculator calculator = new Calculator ();
//new Thread(calculator).start();//if Calculator implements Runnable
calculator.start();//if Calculator extends Thread

new Watcher(calculator,"Th A").start();
new Watcher(calculator,"Th B").start();
new Watcher(calculator,"Th C").start();
//add as many of these Watcher-thread instantiations as you please.
}
}

class Watcher extends Thread
{
Calculator c;
String n;

public Watcher(Calculator calc,String nome)
{
c=calc;
n=nome;
}

public void run()
{
try { Thread.sleep((int)(2*1000)); } catch (InterruptedException e) {}
System.out.println(n + " before synchronizing on [Calculator c] object");
synchronized(c) {
try {
System.out.println(n + " Within synch - will Wait for calculation");
c.wait();
System.out.println(n + " WIthin synch - After waiting for calculation");
} catch (InterruptedException e) {}
}
System.out.println(n + " outside synch - Total value is " + c.total);
}

}

class Calculator extends Thread
// switch between "implements Runnable" and "extends Thread" on the line above
// Do not forget to modify function "main" accordingly.
// Do that... and watch strange difference in program behavior!
{
int total;
public void run()
{

System.out.println("Calculator-before synch on Calculator object");
synchronized(this) {
System.out.println("Calculator-within synched code-will calculate");
for (int i=0; i<Reader.delayIterations; i++)
{
total ++;
File f = new File(Reader.delayDirectory);
File fList[] = f.listFiles();
}
System.out.println("Calculator-done calculating... will notify!");
notify();
System.out.println("Calculator-has notified.");
}
System.out.println("Calculator-outside synch-before an arbitrary delay");
for (int i=0; i<Reader.delayIterations; i++)
{
File f = new File(Reader.delayDirectory);
File fList[] = f.listFiles();
}
System.out.println("Calculator-outside synch-after delay- HERE I DIE");
}
}
 
Reply With Quote
 
 
 
 
Jon Skeet
Guest
Posts: n/a
 
      10-24-2003
Renato Battaglia <(E-Mail Removed)> wrote:
> // JAVA THREADS - A JAVA BUG? Hope not!PLEASE ANYBODY HELP ME! Read on, folks...


Well, I don't get the same behaviour as you. With the code as posted,
it just waits at the end (as it should) when you say it doesn't for
you.

One problem in your understanding though - Thread.sleep doesn't release
any locks. Thread.wait does, but Thread.sleep doesn't.

Which JVM are you using, by the way?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
 
xarax
Guest
Posts: n/a
 
      10-24-2003
"Jon Skeet" <(E-Mail Removed)> wrote in message
news:MPG.1a02e7eaf271a3759896bf@10.1.1.14...
> Renato Battaglia <(E-Mail Removed)> wrote:
> > // JAVA THREADS - A JAVA BUG? Hope not!PLEASE ANYBODY HELP ME! Read on,

folks...
>
> Well, I don't get the same behaviour as you. With the code as posted,
> it just waits at the end (as it should) when you say it doesn't for
> you.
>
> One problem in your understanding though - Thread.sleep doesn't release
> any locks. Thread.wait does, but Thread.sleep doesn't.


Thread.wait? Maybe you mean Object.wait? It will release/reacquire
the monitor lock on the specified object. All other held monitors
are unaffected.


--
----------------------------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!


 
Reply With Quote
 
Jon Skeet
Guest
Posts: n/a
 
      10-24-2003
xarax <(E-Mail Removed)> wrote:
> > One problem in your understanding though - Thread.sleep doesn't release
> > any locks. Thread.wait does, but Thread.sleep doesn't.

>
> Thread.wait? Maybe you mean Object.wait? It will release/reacquire
> the monitor lock on the specified object. All other held monitors
> are unaffected.


Yes indeed I did mean Object.wait. Oops

(But Thread.sleep still doesn't release any monitors

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PLEASE HELP - How to simulate 'wait' and 'notify' in RMI environment cpptutor2000@yahoo.com Java 2 10-09-2005 04:41 AM
wait/notify cpu cost? Jeff Java 5 04-23-2004 11:08 AM
animation and threads, wait - notify??? eeyimaya Java 3 02-18-2004 01:23 PM
Theads problem - wait and notify Abraham Khalil Java 1 09-03-2003 07:07 AM
j2me thread wait/notify question Stee1HeD Java 3 08-12-2003 12:03 AM



Advertisments