wait() notify() problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: wait() notify() problem

Hybrid View

  1. #1
    Satish Guest

    wait() notify() problem



    Pls have a look at the code below, when I am trying use wait() and notify()methods
    , I am getting IllegalMonitorStateException saying that the current thread
    is not owner..what odes it mean and how do I rectify the problem ???



    import com.logica.omg.psa.PsaBindRequest;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.net.SocketException;

    public class PsaSocketNode {

    private final String host = "158.234.96.153"; // JENNY the machine on
    which
    // the PSA Server is running
    private final int port = 3535; //
    private static Socket psaSocket;
    private InputStream inStream;
    private OutputStream outStream;
    private PsaBindRequest psaBindRequest;
    private PsaBindResponse psaBindResponse;
    private byte initFrame;
    private byte control; // bind/unbind or transaction traffic
    private byte service;
    private byte version;
    private long transactionId;
    private byte operation;
    private byte transactionResult;
    private byte transCompRetryPadding; //transaction component + retry +
    padding
    private byte accIndOperParams;
    private SMEAddress sourceAddress; //MSISDN
    private SMEAddress destinationAddress; //MDN
    private SMEAddress sourceMSCAddress;
    private SMEAddress destinationMSCAddress;
    private byte ton;
    private byte npi;
    private String msisdn;
    private byte endFrame;
    private byte response;
    private short protocol;
    private byte[] applRequestData = new byte[105];
    private byte[] bindRequestData = new byte[5];
    private byte[] unbindRequestData = new byte[5];
    private static byte[] responseBindData = new byte[6];
    private static byte[] responseUnbindData = new byte[5];
    private static byte[] responseApplData = new byte[16];

    private boolean gotResponse = false;

    /** Default Constructor */
    public PsaSocketNode() {


    this.connectToServer();
    this.sendCommand();
    this.gotResp();

    }

    public synchronized void sendCommand()
    {
    SendBindRequest sendBindRequest = new SendBindRequest();

    }

    public synchronized void gotResp()
    {

    ReadBindResponse readBindResponse = new ReadBindResponse();
    }


    public void connectToServer() {
    try {
    psaSocket = new Socket(host, port);
    System.out.println("Connected to " + host + " " + "on " + port);
    outStream = psaSocket.getOutputStream();
    inStream = psaSocket.getInputStream();
    }
    catch (UnknownHostException ioe) {
    System.err.println("No local IP address found - is your network
    up?");
    }
    catch (IOException ie) {
    System.err.println("Could not connect to host - IO exception");
    }
    }

    public byte[] constructBindRequest() {
    this.initFrame = 0x7e;
    this.control = 0x01; // bind request
    this.service = 0x01;
    this.version = 0x01;
    this.endFrame = 0x7e;
    psaBindRequest = new PsaBindRequest(this.initFrame, this.control,
    this.service, this.version, this.endFrame);
    System.out.println("Constructed the bind request object");
    byte[] bindRequestData = psaBindRequest.getByteInformation();
    System.out.println("Converted the data into a byte array");
    return bindRequestData;
    }


    public void sendBindRequest() {
    try {
    bindRequestData = constructBindRequest();
    System.out.println("Got the byte array object reference" + bindRequestData.length);
    //outStream = psaSocket.getOutputStream();
    System.out.println("About to write bind data");
    try
    {
    outStream.write(bindRequestData);
    System.out.println("satish sent the bind request");
    }
    catch (SocketException se){System.err.println("Could not write
    the application data on to socket"); };

    System.out.println("PSA CLIENT : Finished writing bind data on
    to the socket");
    }
    catch (IOException e) {
    System.err.println("Could not write the bind data on to socket
    ");
    }
    }
    public void readBindResponse(byte[] responseBindData) {
    try {
    //inStream = psaSocket.getInputStream();
    inStream.read(responseBindData);
    System.out.println("PSA CLIENT : Got the bind response from the
    Server");
    initFrame = responseBindData[0];
    service = responseBindData[1];
    protocol = responseBindData[2];
    version = responseBindData[3];
    response = responseBindData[4];
    endFrame = responseBindData[5];
    // This is the byte that has the response value in it.
    if (response == 0) {
    System.out.println("SUCCESSFUL BIND REQUEST");
    System.out.println("Bind Response Code is " + response);
    }
    else {
    System.out.println("UNSUCCESSFUL BIND REQUEST");
    }
    System.out.println("PSA CLIENT : Finished reading the bind response");
    }
    catch (UnknownHostException ioe) {
    System.err.println("No local IP address found - is your network
    up?");
    }
    catch (IOException ie) {
    System.err.println("Could not read the bind response data from
    the socket");
    }
    }



    class SendBindRequest extends Thread {



    SendBindRequest() {
    this.start();
    }

    public void run() {
    try {
    PsaSocketNode.this.sendBindRequest();
    while(!gotResponse)
    {
    wait();
    System.out.println("satish waiting for the response
    to come");
    }
    }
    catch (Exception e) {System.out.println(e.getMessage()); }
    }
    }


    class ReadBindResponse extends Thread {



    ReadBindResponse() {
    this.start();
    }

    public void run() {
    try {
    PsaSocketNode.this.readBindResponse(responseBindData);
    System.out.println("satish got the bind response");
    gotResponse = true;
    notify();
    System.out.println("satish notifieed the other thread
    abput reponse");

    }
    catch (Exception e) { System.out.println(e.getMessage()); }
    }
    }



    public static void main(String[] args) {
    PsaSocketNode psaSocketNode = new PsaSocketNode();
    }

    }


  2. #2
    Satish Guest

    Re: wait() notify() problem


    The problem in the code below is wait() and notify() must be used with in
    the synchronized blocks. My problem is rectified now, may be this might help
    others.

    Satish

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center