LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2005, 12:45 AM   #1
Miaire
LQ Newbie
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Fedora Core 3
Posts: 11

Rep: Reputation: 0
Implementing a blocking loop (Java or C)


I am not sure about the term I used but what I'm thinking about is something like this. I want to have a loop which will continue to loop until a certain variable, say a boolean becomes change.

Code:
while( locked ){
 // nothing
}
Then on another part of the code, on perhaps a thread, it will set locked to false so the code will break free from the loop and continue on. But I seem to notice that it eats up a lot of processing power, so I set up sleep for a few milliseconds, like in Java:

Code:
while(locked ){
    try{
        Thread.currentThread().sleep(100);
    }
    catch( InterruptedException e ){
    }
}
I was wondering if there's a better way of implementing something like this in Java. Something that is much more punctual than a given amount of time (in that example, 100ms) and something that doesn't consume as much computer processing power. And maybe also something for C because I'm curious. Thanks a lot.

Last edited by Miaire; 04-28-2005 at 12:46 AM.
 
Old 04-28-2005, 01:30 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

This is built into Java. Have a look at the notify() and wait() methods on this page:
http://java.sun.com/j2se/1.5.0/docs/...ng/Object.html

Dave
 
Old 04-28-2005, 01:49 AM   #3
Miaire
LQ Newbie
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Fedora Core 3
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by ilikejam
This is built into Java. Have a look at the notify() and wait() methods on this page:
http://java.sun.com/j2se/1.5.0/docs/...ng/Object.html
Thanks for your reply, I didn't know that was the purpose of it. I implemented it but I'm still left wondering because it throws IllegalMonitorStateException when I use another thread to notify() after I used wait() on another thread. I was wondering if I'm just supposed to catch it.

= Edit: hold on I think I made a mistake somewhere, checking my mistake first, please ignore this.

Last edited by Miaire; 04-28-2005 at 02:05 AM.
 
Old 04-28-2005, 02:09 AM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
No, it shouldn't throw that error. It's ages since I did anything multithreaded in Java, so I'm not sure of the details.

Looking at the description for wait():
Code:
synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(); //you probably want wait(), not wait(int timeout) here
         ... // Perform action appropriate to condition
     }
is something like what you're looking for. To wake the thread, you'd use 'obj.notifyAll()'

Don't quote me on that though...

Dave

Last edited by ilikejam; 04-28-2005 at 02:12 AM.
 
Old 04-28-2005, 02:22 PM   #5
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
In C or C++ you would use some sort of semaphore or mutex. The specific functions to call are dependent upon what "style" of code you are writing. If simple multiprocessing you could use the IPC "semctl()", "semget()", and "semop()" functions. If multithreading (using pthreads) you could use "pthread_mutex_init()", "pthread_mutex_lock()", "pthread_mutex_unlock()", and "pthread_mutex_destroy()" for mutex locks (You could then use those primitives to create your own semaphore library).
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing mmap Kumar Programming 0 09-08-2005 03:12 AM
Java Stopping an Infinite loop oulevon Programming 3 10-18-2004 10:11 PM
Java, implementing Serializable djgerbavore Programming 0 10-01-2004 11:31 AM
Java window not opening when loop included nro Programming 2 09-07-2004 02:16 PM
how to tell if my install is blocking a java URLConnection complus Linux - Newbie 1 10-06-2003 03:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration