Dev Shed Lounge
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherDev Shed Lounge

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:
  #61  
Old June 15th, 2007, 03:10 PM
gimp's Avatar
gimp gimp is offline
<?PHP user_title("gimp"); ?>
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2005
Location: Internet
Posts: 6,019 gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)  Folding Points: 1555 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 1 h 55 m 25 sec
Reputation Power: 1815
Send a message via AIM to gimp
So I'm looking for a bit of advice.

I have several threads. Since these need to keep running forever, or until stuff is shut off, there are infinite loops. Well, wouldn't you know it, that makes my simple little console app eat CPU like Oprah eats cheesecake.

What would be much better is if the thread could block until it was needed again. Instead of constantly doing stuff (looping), it should do nothing until it needs to.

I'm going to google it and post back if I get a good solution.
__________________
A work in progress: Card Game Platform (Status: Hard Drive Crash deleted project, rewrite planned) | Joke Thread
“Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was
its finest fruit.”

Reply With Quote
  #62  
Old June 15th, 2007, 03:41 PM
tagmanadvance's Avatar
tagmanadvance tagmanadvance is offline
Kage Bunshin
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2005
Location: The Seven Seas Of Rhye
Posts: 779 tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 16 h 22 m 3 sec
Reputation Power: 180
Send a message via AIM to tagmanadvance Send a message via Yahoo to tagmanadvance Send a message via XFire to tagmanadvance
Setup a system of checks. If A is doing X, have B do Y. Otherwise have B sleep for Z seconds. Of course, there is probably a better solution, so keep looking.
__________________
"Java makes impossible things possible, but makes easy things difficult." - Somebody

Reply With Quote
  #63  
Old June 15th, 2007, 04:12 PM
Axweildr's Avatar
Axweildr Axweildr is online now
CPAN medic ...
Dev Shed God 13th Plane (11000 - 11499 posts)
 
Join Date: Mar 2003
Location: Location: Location:
Posts: 11,413 Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)Axweildr User rank is General 27th Grade (Above 100000 Reputation Level)  Folding Points: 125219 Folding Title: Super Ultimate Folder - Level 1Folding Points: 125219 Folding Title: Super Ultimate Folder - Level 1Folding Points: 125219 Folding Title: Super Ultimate Folder - Level 1Folding Points: 125219 Folding Title: Super Ultimate Folder - Level 1Folding Points: 125219 Folding Title: Super Ultimate Folder - Level 1Folding Points: 125219 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 2 Days 4 h 59 m 4 sec
Reputation Power: 2812
Send a message via Google Talk to Axweildr
Orkut
You up to speed with semaphores for interprocess communications?
__________________
--Ax
without exception, there is no rule ...
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


09 F9 11 02
9D 74 E3 5B
D8 41 56 C5
63 56 88 C0
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
-- Jamie Zawinski
Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...

Reply With Quote
  #64  
Old June 15th, 2007, 04:28 PM
gimp's Avatar
gimp gimp is offline
<?PHP user_title("gimp"); ?>
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2005
Location: Internet
Posts: 6,019 gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)  Folding Points: 1555 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 1 h 55 m 25 sec
Reputation Power: 1815
Send a message via AIM to gimp
Quote:
Originally Posted by tagmanadvance
Setup a system of checks. If A is doing X, have B do Y. Otherwise have B sleep for Z seconds. Of course, there is probably a better solution, so keep looking.
Goddamn. Do you not think that was the most obvious solution? I identified the problem, the solution came within a second. It's obvious it works, but sh*ttily. Do you have any idea of the kind of lag that would be cause if the program slept that often? I'm looking for a good solution. I may have found one.

Reply With Quote
  #65  
Old June 15th, 2007, 05:09 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Stubborn ol' L'User
Click here for more information.
 
Join Date: Jun 2005
Posts: 3,422 sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level)sizablegrin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 52 m 32 sec
Reputation Power: 1731
Makes one wish for a good OS where one can relinquish one's time slice.

If you have a lot of threads that do nothing for a lot of the time, perhaps you should reconsider your approach. Spawn a thread when you need it, grab the results, and kill it.
Comments on this post
tagmanadvance agrees!

Reply With Quote
  #66  
Old June 15th, 2007, 05:30 PM
tagmanadvance's Avatar
tagmanadvance tagmanadvance is offline
Kage Bunshin
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2005
Location: The Seven Seas Of Rhye
Posts: 779 tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 16 h 22 m 3 sec
Reputation Power: 180
Send a message via AIM to tagmanadvance Send a message via Yahoo to tagmanadvance Send a message via XFire to tagmanadvance
Quote:
Originally Posted by gimp
Goddamn. Do you not think that was the most obvious solution? I identified the problem, the solution came within a second. It's obvious it works, but sh*ttily. Do you have any idea of the kind of lag that would be cause if the program slept that often? I'm looking for a good solution. I may have found one.

'twas only a temporary suggestion, until you could come up with a better solution. Sleep uses up no CPU time, so it would work perfectly to reduce stress on the CPU. If sizablegins solution doesn't suit you, maybe try something like THIS?

Reply With Quote
  #67  
Old June 15th, 2007, 06:53 PM
gimp's Avatar
gimp gimp is offline
<?PHP user_title("gimp"); ?>
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2005
Location: Internet
Posts: 6,019 gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)  Folding Points: 1555 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 1 h 55 m 25 sec
Reputation Power: 1815
Send a message via AIM to gimp
As I said, I think I got something. To be perfectly honest, almost all 'advice' you have given me absolutely does not help. Please review your posts and see for yourself. And have at least some server/client experience before telling me how to do things. This is a soft rebuke, but mostly asking you to know before speaking. The link you provided me is some very unsafe coding that might just work if it was what I wanted. Incidentally, a 1-second glance through the API had already told me that a long time ago.

Wei: I really don't have that many. One thread for every user. One processing thread. One thread for each game. With 10 users and 10 games (of solitaire?) I'd have 21 threads. With 20 people and 5 games of setback, I'd have 26 threads. Too many? The thing is, I know my server can handle it. I've made it have over 25 threads at a time and it didn't balk. My problem is that it consumes all the CPU it can, no matter whether there are 20 people connected or none. Take a look: Driver class creates Server class. The Server controls everything else. Take a look at Server's constructor. More importantly, take a look at the try{} block. That's why it'll eat all CPU it can get even without users connected.

JAVA Code:
Original - JAVA Code
  1. public Server() {
  2.     /** Processing thread */
  3.     processor = new ProcessThread(this);
  4.     processor.start();
  5.    
  6.     /** Connections table */
  7.     connections = new HashMap<String, Branch>();
  8.    
  9.     /** Create game */
  10.     games = new HashMap<String,Game>();
  11.     gameNames = new ArrayList<String>();
  12.    
  13.     System.out.println("Server started.");
  14.    
  15.     try {
  16.         /** Port 9413 */
  17.         myService = new ServerSocket(9413);
  18.         while (true) {
  19.             /** Accept new connection, give it its own thread */
  20.             Branch b = new Branch(myService.accept(), this);
  21.             b.createTempUser(); b.start();
  22.             connections.put(b.getUsername(), b);
  23.            
  24.             System.out.println("Created new branch. Temp username: " + b.getUsername());
  25.            
  26.             connectionsMade++;
  27.         }
  28.     }
  29.     catch (IOException e) {
  30.         System.out.println(e);
  31.     }
  32. }

Reply With Quote
  #68  
Old June 15th, 2007, 06:57 PM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,385 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 72870 Folding Title: Intermediate FolderFolding Points: 72870 Folding Title: Intermediate FolderFolding Points: 72870 Folding Title: Intermediate FolderFolding Points: 72870 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 20 h 22 m 9 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
Maybe it's time to move this over to the Java forum? ...

Reply With Quote
  #69  
Old June 15th, 2007, 07:01 PM
gimp's Avatar
gimp gimp is offline
<?PHP user_title("gimp"); ?>
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2005
Location: Internet
Posts: 6,019 gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)gimp User rank is General 13rd Grade (Above 100000 Reputation Level)  Folding Points: 1555 Folding Title: Novice Folder
Time sp