Welcome to Dream.In.Code
Become a Java Expert!

Join 150,131 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,129 people online right now. Registration is fast and FREE... Join Now!




waitForState(Processor p, int state)

 
Reply to this topicStart new topic

waitForState(Processor p, int state), Waiting for a processor to be configured in jmf

elzajose
3 Aug, 2008 - 09:03 PM
Post #1

New D.I.C Head
*

Joined: 19 Jul, 2008
Posts: 4

Can u explain this code:

java

private synchronized boolean waitForState(Processor p, int state) {
p.addControllerListener(new StateListener());
failed = false;

// Call the required method on the processor
if (state == Processor.Configured) {
p.configure();
} else if (state == Processor.Realized) {
p.realize();
}

// Wait until we get an event that confirms the
// success of the method, or a failure event.
// See StateListener inner class
while (p.getState() < state && !failed) {
synchronized (getStateLock()) {
try {
getStateLock().wait();
} catch (InterruptedException ie) {
return false;
}
}
}

if (failed)
return false;
else
return true;
}

*edit: Please use code tags in the future, thanks! code.gif

This post has been edited by Martyr2: 3 Aug, 2008 - 09:14 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: WaitForState(Processor P, Int State)
3 Aug, 2008 - 09:36 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well without knowing all the code behind Processor or the listener or getStateLock I can say that this appears to be checking for the state of the processor object and monitors the object until it is available and gets a lock.

This function appears to check the state of the object and if it is not in the state specified, it keeps checking until it is. This function then returns true if the object is now in the specified state or false if there was an error.

Since this appears to be dealing with synchronization it is to be assumed that it is waiting for the object to be free of a lock so that another thread can get access to it.

If you are unfamiliar with locks, threads at times may attempt to use the same object at the same time. To prevent this a thread can place a lock on the object which makes all other threads wait to use it. After it is done with the object, it removes the lock and thus allows waiting threads to have access.

But other than that, no one can be sure without seeing the other functions and the Processor object.

smile.gif

This post has been edited by Martyr2: 3 Aug, 2008 - 09:37 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:46AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month