java - Wrong thread waiting -
i'm trying read data socket, thread not thread reading. second thread listens commands being sent. want thread listens commands wait until i'm done on second thread. problem command listening thread not waiting, instead other thread want listen waits.
this method being run thread don't want wait
public void sendmessage(message message) { pauselistener(true); try { //inform receiver message sent system.out.println("sending message"); sendfunction(function.snd_message); //and wait response, ok allow sending of data system.out.println("awaiting confirmation"); error status = readerror(); if(status == error.ok) { system.out.println("sending message data"); //send data sendbytes(message.getsender().getbytes(), 0); byte[] bytelong = long.tostring(message.getid()).getbytes(); sendbytes(bytelong, 0); sendbytes(message.getdata().getbytes(), 0); senderror(error.ok); } else { //todo error handling system.err.println("failed send message: " + status.name()); } } catch (exception e) { //todo error stuff system.err.println("an error occured sending message"); e.printstacktrace(); } { pauselistener(false); } } the method pause listener meant cause listen wait, i'm pretty new working threads advice appreciated. self.wait() set instance of thread want wait
public void pauselistener(boolean pause) { synchronized (self) { try { if(pause) { self.wait(); } else { self.notify(); } }catch(interruptedexception e) { system.err.println("failed suspend listener on client:" + socket); e.printstacktrace(); } } } this run() of thread i'm trying wait.
public void run() { //listen commands self = thread.currentthread(); while(true) { system.out.println("awaiting instruction"); function function; try { function = readfunction(); } catch (unkowncommandexception e) { e.printuserfriendly(); e.printstacktrace(); continue; //skip unknown function } switch (function) { case snd_message: //prepare receive message system.out.println("receiving message"); message input = readmessage(); if(server != null) { server.broadcast(input); } else { system.out.println(input.tostring()); } break; default: system.out.println("unknown function: " + function.name()); break; } } } if need more information can give it. appreciated, fresh pair of eyes.
Comments
Post a Comment