Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Deadlock... i think

Thread view: 
Carsten H. Pedersen - 18 Feb 2006 10:42 GMT
I apologize in advance for the length of the note, and the source code
included.

I posted earlier under the title "Double streams", where i wanted to
use an RMI object as a sort of proxy for two clients to exchange data
via streams. I kinda failed, but now i'm trying something new. This
doesn't work either, but this time i think someone might be able to
solve it, since i'm not adept at using threads and i think that's
where the problem is. :)

The source for ByteHolder and RemoteByteHolder is included below. A
server is run, making a ByteHolder, BH, available via RMI.

Client A obtains the reference to BH, and passes this on to its
FooOutputStream, which extends OutputStream and implements the
write(int b) method by calling putByte(b) on BH.

Client B also gets BH, passes this to FooInputStream, which extends
InputStream and implements read() by calling getByte() on BH.

What i did to test was:
 - run the server
 - client A wraps the FooOutputStream in a PrintWriter, and
println("foo") is called on it
 - client B wraps FooInputStream in a InputStreamReader, which is
then wrapped in a BufferedReader, and readLine() is called on that

The data then goes, one byte at the time, from client A to client B.
Client A stops running, but Client B just sorta hangs around... in a
deadlock i presume. I can make it return and print out the read line
by doing one of two things:
 - kill the server
 - manually send a -1 to client B

When i've used streams before, i didn't have to send a -1 in order to
get the reading side to get on with it - it just happened magically. I
also don't understand why killing the server make things better...

Can anyone explain this or, as an alternative, suggest a better way to
exchange bytes via RMI. I just really wanted to make streams work. :)

Oh, another thing. To make the server available, i've made it Runnable
and in the run i have a while(true) loop, that makes the thread sleep
for a chunk of time. Maybe that is a problem, too?

Regards,
Carsten H. Pedersen

----------- SOURCE BELOW -------------

package streams.holder;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RemoteByteHolder extends Remote {
  int getByte() throws RemoteException;
  void putByte(int b) throws RemoteException;
}

...

package streams.holder;
import java.rmi.RemoteException;

public class ByteHolder implements RemoteByteHolder {

  private int theByte;
  private boolean stored = false;

  public synchronized int getByte() throws RemoteException {
    while(!stored) {
      try {
        wait();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    System.out.println("hb: get: "+theByte);
    int i = theByte;
    stored = false;
    notifyAll();
    return i;
  }

  public synchronized void putByte(int b) throws RemoteException {
    while(stored) {
      try {
        wait();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    System.out.println("bh: put: "+b);
    stored = true;
    theByte = b;
    notifyAll();
  }

}
Oliver Wong - 20 Feb 2006 16:06 GMT
> I posted earlier under the title "Double streams", where i wanted to use
> an RMI object as a sort of proxy for two clients to exchange data via
> streams. I kinda failed, but now i'm trying something new. This doesn't
> work either, but this time i think someone might be able to solve it,
> since i'm not adept at using threads and i think that's where the problem
> is. :)

   If you don't get any answers for 1 or 2 days, try posting this to
comp.lang.java.programmer. There's a lot of people there who are familiar
with threading who don't read this newsgroup.

   If you post there too early though, you get accused of multiposting.

   - Oliver
Godspeed - 01 Mar 2006 12:38 GMT
Hi Carsten,

What does your readLine code look like?

Are you doing something like:

while ((line = readLine())!=null) ...

>I apologize in advance for the length of the note, and the source code
>included.
[quoted text clipped - 100 lines]
>
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.