SunQuest
           Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old May 3rd, 2008, 06:29 PM
Chucara Chucara is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 2 Chucara User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 37 sec
Reputation Power: 0
Threads and Condition.await

I'm trying to get better acquainted with threaded programming by building a mock music player. It's not going over so well

What am I doing wrong in the code snipped below? The snippet below is inside the run() method of the MusicPlayer class.
It seems that it simply doesn't excute as intended. The behavior I want is:
- When in Off mode: do nothing
- When in Playing mode: Wait until song finishes, then remove song from playlist (call PlaySong)
- When in Pause mode: Wait (pauseTimeOut) milliseconds, and if nothing happens, go to Off mode.

The thread runs fine, but it seems that it waits forever in whatever state it is in.

I apologize for the weird if statements..

Code:
while (alive) {
			System.out.println("State: "+state);
			switch (state) {
			case Off:
				System.out.println("System is off");
				try { cond.await(); }
				catch(InterruptedException e) { alive = false; }
				break;
			case Paused:
				boolean stillWaiting=true;
				System.out.println("Waiting..");
				try { stillWaiting=cond.await(pauseTimeOut, TimeUnit.MILLISECONDS); }
				catch(InterruptedException e) { alive = false; }
				if(stillWaiting) {
					//Do nothing
				} else if (state == States.Paused) {
					state=States.Off;
				}
				break;
			case Playing:
				System.out.println("Playing..");
				if(playlist == 0) {
					state = States.Paused;
				} else {
					boolean waitingEndSong=true;
					try { waitingEndSong=cond.await(songTime, TimeUnit.MILLISECONDS); }
					catch(InterruptedException e) { alive = false; }
					if(waitingEndSong) {
						//Do nothing
					} else if (state == States.Playing) {
						PlaySong();
					}
					break;
				}
				
				try { cond.await(); }
				catch(InterruptedException e) { alive = false; }
				break;
			default:
				alive = false;
			}
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Threads and Condition.await


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway