GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Apr-2006, 21:29
smoothdogg00 smoothdogg00 is offline
Junior Member
 
Join Date: Mar 2006
Posts: 31
smoothdogg00 is on a distinguished road
Unhappy

Java threads trouble (wait() and notify())


I have a main program, which starts two separate threads Sender and Receiver. Those threads extend Thread, and they contain the following:

main (Server.java)
JAVA Code:
public void main(){
  receiver = new Receiver();
  receiver.start();

  sender = new Sender();
  sender.start();
}
Receiver
JAVA Code:
public void run(){
  while(true)
  {
    wait();
    DO STUFF
    Server.sender.notify();
  }
}
Sender
JAVA Code:
public void run(){
  while(true)
  {
    Server.receiver.notify();
    wait();
    DO STUFF
  }
}
As you can see, I would like the receiver to do all of its stuff first, then have the sender do all of its stuff, and have them keep trading off in an infinite loop. The error I'm getting is:
JAVA Code:
Exception in thread "Thread-2" java.lang.IllegalMonitorStateException: current thread not owner

Thanks,
Bill
  #2  
Old 08-Apr-2006, 13:16
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 995
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Java threads trouble (wait() and notify())


Can you possibly share more source of what you're trying to do? It is not clear if "DO STUFF" might be sharing any functionality.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 08-Apr-2006, 14:50
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 995
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Java threads trouble (wait() and notify())


I found a reference that says notify() [and notifyAll() for a ThreadGroup] can only be used within synchronized code. That why I am curious about "DO STUFF".
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #4  
Old 08-Apr-2006, 16:45
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 995
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Java threads trouble (wait() and notify())


Never mind your source. I came up with this for an example:
JAVA Code:
public class ThreadPlay
{
	public static void main(String args[])
	{
		SynchronizedReceiverAndSender s =
			new SynchronizedReceiverAndSender();
		Receiver receiver = new Receiver(s);
		Sender sender = new Sender(s);

		System.out.println("Starting threads...");
		receiver.start();
		sender.start();
		System.out.println("Threads started!");
	}
}

class Receiver extends Thread
{
	private SynchronizedReceiverAndSender receiverRun;

	public Receiver(SynchronizedReceiverAndSender s)
	{
		super("RECEIVER:");
		receiverRun = s;
	}

	public void run()
	{
		while (true)
		{
			try
			{
				Thread.sleep( 2500 );
			}
			catch (InterruptedException ie )
			{}

			receiverRun.processReceiver();
		}
	}
}

class Sender extends Thread
{
	private SynchronizedReceiverAndSender senderRun;

	public Sender(SynchronizedReceiverAndSender s)
	{
		super("SENDER:");
		senderRun = s;
	}

	public void run()
	{
		while (true)
		{
			try
			{
				Thread.sleep( 2500 );
			}
			catch (InterruptedException ie )
			{}

			senderRun.processSender();
		}
	}
}

class SynchronizedReceiverAndSender
{
	// initializing false forces the 'Sender' to wait first.
	// change to true will make the 'Receiver' wait first.
	private boolean myTurn = false;

	public synchronized void processReceiver()
	{
		while (myTurn) // not the Sender's turn.
		{
			try
			{
				wait();
			} catch (InterruptedException ie ) {}
		}

		// your DO stuff here
                System.out.println(Thread.currentThread().getName() +
							" process info...");

		myTurn = true;
		notify();
	}

	public synchronized void processSender()
	{
		while (!myTurn) // not the Receiver's turn.
		{
			try
			{
				wait();
			} catch (InterruptedException ie ) {}
		}

                // your DO STUFF here.
		System.out.println(Thread.currentThread().getName() +
							" send info. to receiver");

		myTurn = false;
		notify();
	}
}

... and here's the console output...

Starting threads...
Threads started!
RECEIVER: process info...
SENDER: send info. to receiver
RECEIVER: process info...
SENDER: send info. to receiver
RECEIVER: process info...
SENDER: send info. to receiver
RECEIVER: process info...
SENDER: send info. to receiver
...
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.