Carlos,
Your problem is with Thread.currentThread().wait()/notifyAll(). Since your method is
synchronized, you own the monitor for "this", not the monitor for currentThread()
(every object has its own monitor).
Try removing "Thread.currentThread()." and leaving just the "wait()/notifyAll()"
calls, and see if it does what you want. If you want to grab the monitor for some
other object, instead of synchronizing the whole method you can use a synchronized
block:
synchronized (anObject) {
// ... blah blah blah
anObject.notifyAll()
}
Hope this helps,
-Mario Camou.
Carlos Alberto Roman Zamitiz wrote:
> Hi, I attached my Cliente.java
> I have 2 threads: Receptor and Transmisor. Receptor sends and receives
> data from server, using method recibirPeticion(), forever (well, it sleeps
> 3 seconds). When Transmisor sends data, using method enviarPeticion(),
> Receptor must wait until Transmisor ends his transmition.
>
> I get
> java.lang.IllegalMonitorStateException: current thread not owner
>
> What's wrong?
>
> Thanks!
>
> Carlos
>
> --------------------------------------------------------------------------------
> import java.io.BufferedReader;
> import java.io.PrintWriter;
> import java.io.IOException;
> import java.io.InputStreamReader;
> import java.net.InetAddress;
> import java.net.Socket;
> import java.net.UnknownHostException;
>
> public class Cliente implements Runnable
> {
> static String respuesta="0";
> static Socket socket=null;
> static PrintWriter out=null;
> static BufferedReader in=null;
> static Thread receptor;
> static Thread transmisor;
> static String host=null;
> static boolean indicador=false;
>
> public static void main(String args[])
> {
> new Cliente();
> }
>
> public Cliente()
> {
> if(true)
> {
> try
> {
> host="132.248.59.4";
> socket=new Socket(InetAddress.getByName(host),4444);
> System.out.println("Creando hilo Receptor");
> receptor=new Thread(this,"Receptor");
> System.out.println("Creando hilo Transmisor");
> transmisor=new Thread(this,"Transmisor");
> receptor.start();
> transmisor.start();
> }
> catch(UnknownHostException e)
> {
> System.err.println("Host desconocido: "+host);
> System.exit(1);
> }
> catch(IOException e)
> {
> System.err.println("Error de Entrada/Salida con: "+host);
> System.exit(1);
> }
> }
> }
>
> public void run()
> {
> while(true)
> {
> if(Thread.currentThread().getName().equals("Receptor"))
> recibirPeticion();
> else if(Thread.currentThread().getName().equals("Transmisor"))
> enviarPeticion();
> }
> }
>
> public synchronized static void recibirPeticion()
> {
> System.out.println("Iniciando Receptor");
> try
> {
> while(indicador==true)
> {
> System.out.println("Deteniendo hilo Receptor");
> Thread.currentThread().wait();
> System.out.println("Continuando hilo Receptor");
> }
> System.out.println("Receptor recibe estado de la sala");
> out=new PrintWriter(socket.getOutputStream(),true);
> out.println("recibir");
> in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
> respuesta=in.readLine();
> System.out.println("La respuesta del Servidor es: "+respuesta);
> Thread.sleep(3000);
> }
> catch(Exception e)
> {
> System.out.println("Paso algo con el servidor: "+host);
> e.printStackTrace();
> }
> }
>
> public synchronized static void enviarPeticion()
> {
> indicador=true;
> System.out.println("Iniciando Transmisor");
> try
> {
> out=new PrintWriter(socket.getOutputStream(),true);
> System.out.println("El usuario carlos aparto una computadora");
> out.println("enviar");
> out.println("carlos");
> out.println("15");
> out.println("21:00");
> out.println("50");
> }
> catch(IOException e)
> {
> System.err.println("Error de Entrada/Salida con: "+host);
> e.printStackTrace();
> }
> indicador=false;
> System.out.println("Notificando...");
> Thread.currentThread().notifyAll();
> }
> }