[SOLVED] Wait a singal/message from thread in Java
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
for (int x = 0; x < clientes.length; x++)
{
new Thread(new Mensajero(clientes[x], contenido, tipo, archivo.getBytes())).start();
}
It works fine, but inside the thread, this use a device that is blocked for a few seconds ([2, 3, 4 or 6] variable), but during that time the process created more threads and an exception is thrown [Device Busy].
Question
How can I make the process that launches the thread wait for a signal to continue creating more threads, with the assurance that the device is free.
The device is released (inside the thread) when a function returns an id.
i imagine you dont mean to make caller (for loop) wait until the new thread is done, right? if thats the case, then a simple method call will work, and theres no need to do threading. however, i bet this isnt the case, so...
you could modify the signature of the class your trying to run ("Menasajero") to accept another argument, a "device free" flag. for example:
Code:
for (int x = 0; x < clientes.length; x++)
{
// create a reference-type "wrapper" around the primitive boolean, so we can use it to pass by reference and modify
boolean[] deviceFree = {false};
new Thread(new Mensajero(clientes[x], contenido, tipo, archivo.getBytes(), deviceFree)).start();
while ( deviceFree[0] == false )
{
// you could put a sleep in here, or use a "timeout" counter, ie "if ive waited for 10 seconds, continue anyway or an error occured" ,etc
}
}
and modify the "Mensajero" constructor so that it switches this flag when it is done with the device (or whatever it is you need to wait for). if you cant modify this class, then create a runnable wrapper class around it with constructor:
Code:
public MensajeroRunner(Mensajero m, boolean[] deviceFree);
Last edited by nadroj; 09-08-2009 at 06:13 PM.
Reason: added colouring to easily see code change
The solution I've used in the past is to create a single thread which interacts with the device and a thread-safe queue which holds command objects designed to operate the device API. This thread will wait for something to arrive in the queue, dequeue and execute the command object (which will exercise the device API). Any other thread that wishes to use the device must not use it directly, but instead insert a command object into the queue. This design will allow any thread to use the device via the command object API. Since only a single thread "owns" the device the contention issues are less of a problem (centered more in the realm of the queue operation anyway).
You can use java.util.Vector for the queue (using add() to add at the end, and get(0) to fetch from the beginning). The Vector class is already fully synchronized. You should perform a wait() in the device thread when the queue is empty and use notify() to wake the wait()'ing thread up as part of the enqueue operation.
And while the open didn't returns, the device is locked. What I want is that right after the open's call return, the thread send a signal to the parent process that the device is free and can continue creating [in the loop] creating another thread.
Note: I can not wait until the thread ends because as it can take a long time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.