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 09-08-2009, 02:37 PM   #1
scmbg
Member
 
Registered: Oct 2008
Location: Mexico City
Distribution: Fedora
Posts: 65

Rep: Reputation: 15
Question Wait a singal/message from thread in Java


I have the following code.

Code:
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.
 
Old 09-08-2009, 06:11 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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
 
Old 09-09-2009, 10:10 AM   #3
kellinwood
Member
 
Registered: Jan 2006
Location: Culver City, California
Distribution: Fedora
Posts: 64

Rep: Reputation: 21
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.

See also: http://en.wikipedia.org/wiki/Command_pattern

Ken

Last edited by kellinwood; 09-09-2009 at 10:20 AM. Reason: Added queue implementation hints. Command pattern link.
 
Old 09-09-2009, 10:36 AM   #4
scmbg
Member
 
Registered: Oct 2008
Location: Mexico City
Distribution: Fedora
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks for the help.

Explains more:

In the Mensajero class, the thread, I have this code:

Code:
ClientSessoin cs = (ClientSession)Conector.open(url);
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.
 
Old 09-09-2009, 10:54 AM   #5
kellinwood
Member
 
Registered: Jan 2006
Location: Culver City, California
Distribution: Fedora
Posts: 64

Rep: Reputation: 21
OK, now I think I understand.

In the parent, do this... (note I've added the reference to the thread creation object, i.e., 'parent')
Code:
for (int x = 0; x < clientes.length; x++)
{
	new Thread(parent, new Mensajero(clientes[x], contenido, tipo, archivo.getBytes())).start();
        synchronized( this) {
           try {
              wait();
           }
           catch (InterruptedException x) {
              System.out.println( x.toString());
           }
       }
}
In the thread, do this:

Code:
ClientSessoin cs = (ClientSession)Conector.open(url);
synchronized( parent) { parent.notify(); }
This way a new thread will only be created after the open call returns in the child threads.
 
Old 09-09-2009, 12:09 PM   #6
scmbg
Member
 
Registered: Oct 2008
Location: Mexico City
Distribution: Fedora
Posts: 65

Original Poster
Rep: Reputation: 15
Thank you!!
 
  


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
IN JAVA, how to do a multi chat client in java with the option of private message ? nicolasd Programming 5 09-16-2009 08:53 PM
How long do I have to wait for thread approval? Kardell LQ Suggestions & Feedback 4 06-30-2009 08:40 AM
Java threads trouble (wait() and notify()) smoothdogg00 Programming 4 04-09-2006 12:05 PM
java multithreading wait for all threads to die true_atlantis Programming 1 10-01-2004 06:26 PM
Java: wait until first dialog is closed before displaying the other one? gundelgauk Programming 2 08-13-2004 06:57 AM

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

All times are GMT -5. The time now is 09:30 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