help on threads/monitor needed

F

Frank J?ger

Hi all,
I am using the javax communication API to talk to RS232. Now this
works fine by using the serialPortEventListener but I always wait for
500 ms to read/write new data. I have made my classes Runnable to put
them into threads and use synchronized to let one thread finish
reading and writing.
To control the synchronized methods I use one attribute (public
boolean) which is set true when initializing the eventListener and set
to false if the string I expect was captured.
If I start several threads (and don't wait 500 ms in between) only one
thread is processed and all others are waiting for ever. Looks like a
deadlock.
How can I resolve this. Is it o.k. to use just one parameter for
controlling all synchronized methods?

Frank
 
B

ByteCoder

Frank said:
Hi all,
I am using the javax communication API to talk to RS232. Now this
works fine by using the serialPortEventListener but I always wait for
500 ms to read/write new data. I have made my classes Runnable to put
them into threads and use synchronized to let one thread finish
reading and writing.
To control the synchronized methods I use one attribute (public
boolean) which is set true when initializing the eventListener and set
to false if the string I expect was captured.
If I start several threads (and don't wait 500 ms in between) only one
thread is processed and all others are waiting for ever. Looks like a
deadlock.
How can I resolve this. Is it o.k. to use just one parameter for
controlling all synchronized methods?

Frank

I'd make a synchronized method in the Class that starts the threads and
give 'this' (= the Class that starts the threads) to the thread so that
it can access the method in the starter thread. That's all you need. :)

Don't forget to create a private object of your Class in the Thread
Class and assign (or was it declare) it in the constructor of the Thread
Class.
 
J

John C. Bollinger

Frank said:
Hi all,
I am using the javax communication API to talk to RS232. Now this
works fine by using the serialPortEventListener but I always wait for
500 ms to read/write new data. I have made my classes Runnable to put
them into threads and use synchronized to let one thread finish
reading and writing.
To control the synchronized methods I use one attribute (public
boolean) which is set true when initializing the eventListener and set
to false if the string I expect was captured.
If I start several threads (and don't wait 500 ms in between) only one
thread is processed and all others are waiting for ever. Looks like a
deadlock.
How can I resolve this. Is it o.k. to use just one parameter for
controlling all synchronized methods?

I don't understand how you are controlling synchronized methods via a
boolean variable. I suspect Java synchronization may not work the way
you think it works.

I also don't understand your communication protocol. Why do you need
multithreaded access to a serial port? Is the serial line multiplexed
or something? Even if it is, it doesn't make sense to me to have
multiple threads listening directly for serial events. Create a class
for a shared object that has the responsibility for managing a single
serial port. A single instance of this class will be the only listener
for serial events on any particular port, and the only object that reads
or writes directly to the port. The threads will be expected to
synchronize on this shared object in order to use the port.


John Bollinger
(e-mail address removed)
 
B

ByteCoder

I have not made it clear enough in my first posting, that I don't want
to wait, say 500 ms, in between tasks. My reason for using threads is
to make the application save and to do a fast initialization which does
not take, say 10 * 500 ms.

Ideally the GUI should not interfere (only interrupt, but not stop)
taking data from RS232, even if some user heavily uses it. So my vision
is to use one lock which I can use for all threads including AWT and
swing threads. Is this possible?

Why would you want more than one thread that manages the RS232?
Why would more than one thread be necessary when there is only one RS232
device/input?

Just start one thread from your swing application that initializes the
communication and read data from it from your swing application.

You could give the communications thread a textfield or something like
that when it starts and whenever the communications thread reads a line
it adds that line to the textfield.
 
B

ByteCoder

ByteCoder wrote:
[...]
You could give the communications thread a textfield or something like
that when it starts and whenever the communications thread reads a line
it adds that line to the textfield.

Actually, that's not a good idea.

Like I said before, just let a thread execute a public synchronized
method in the swing app. That won't affect the performance of your app
in any noticable way.
 
F

frank.jaeger

Thanks again ByteCoder!
The reason for using different classes (which are 'Runnable') is to
handle different Strings comming/going to RS232, as I told before. The
structure of the Strings are quite different, which makes it too
difficult to handle in one serialEvent method.
Could somebody answer to my questions, which I guess are elemantary to
all thread freaks out there:
Once again:
Can I use synchronized (applied to methods) applied in several classes,
using one lock (my boolean parameter which I set true and false in the
synchronized methods)? My access to the lock would go through the
argument passed in the constuctor, to make my lock parameter 'visible'.

snippet:
public class T1 implements ... {
....
private SerialConnection connection;

//constructor
public T1(SerialConnection connection){
this.connection = connection;
}


public synchronized void serialEvent(SerialPortEvent e) {
....

connection.threadActive = false; // is this possible?
}
}//class

In the calss SerialConnection I have declared the attribute
public volatile boolean threadActive; // I use this as lock
which is set true in another synchronized method before entering the
serialEvent method above

Frank
 
B

ByteCoder

Thanks again ByteCoder!
The reason for using different classes (which are 'Runnable') is to
handle different Strings comming/going to RS232, as I told before. The
structure of the Strings are quite different, which makes it too
difficult to handle in one serialEvent method.
Could somebody answer to my questions, which I guess are elemantary to
all thread freaks out there:
Once again:
Can I use synchronized (applied to methods) applied in several classes,
using one lock (my boolean parameter which I set true and false in the
synchronized methods)? My access to the lock would go through the
argument passed in the constuctor, to make my lock parameter 'visible'.

snippet:
public class T1 implements ... {
...
private SerialConnection connection;

//constructor
public T1(SerialConnection connection){
this.connection = connection;
}


public synchronized void serialEvent(SerialPortEvent e) {
...

connection.threadActive = false; // is this possible?
}
}//class

In the calss SerialConnection I have declared the attribute
public volatile boolean threadActive; // I use this as lock
which is set true in another synchronized method before entering the
serialEvent method above

Frank

From your above statements I get the idea you don't really know what
the synchronized keyword does.

The synchronized keyword is used to prevent any other thread calling the
synchronized methods/blocks (that's *all* the synchronized methods in a
class) when one thread is using them. If that one thread is done (the
method completed) the other threads can use the method again on a first
come, first serve basis (they don't 'wait in line'). The thread that's
waiting who 'gets there first' can execute the method.
This all happens automatically. You only have to specify the
synchronized keyword before a (or more) method(s).

Example:

public synchronized void displayString(String message) {
//...
}

If another thread would call this method, but you already are setting
the text using this method the other thread will wait until you are done
setting the text.
 
F

frank.jaeger

This cannot be the whole story, because without the wait() inside the
synchronized method I get the WaitCommEvent: Error 6 and with the
wait() built into the while block
while(lock-variable){
try{
wait();
}catch(InterruptedException ex){}
}
the program freezes after the first thread finished execution.
All other threads also listen to this lock-variable. If I do some
System.out.println on the current thread I see my lock-variable turn
false after the first thread finished but this has no effect to the
other threads waiting
involved are
AWT-EventQueue-0
Win32SerialPort Notification thread
and it looks like the lock cannot be carried from 2nd to 1st

Frank
 
B

ByteCoder

This cannot be the whole story, because without the wait() inside the
synchronized method I get the WaitCommEvent: Error 6 and with the
wait() built into the while block
while(lock-variable){
try{
wait();
}catch(InterruptedException ex){}
}

Now I see (maybe). You should first assign false to the lock-variable
and then you *MUST* interrupt/notify the thread.
the program freezes after the first thread finished execution.
All other threads also listen to this lock-variable. If I do some
System.out.println on the current thread I see my lock-variable turn
false after the first thread finished but this has no effect to the
other threads waiting
involved are
AWT-EventQueue-0
Win32SerialPort Notification thread
and it looks like the lock cannot be carried from 2nd to 1st

No offense, but please give me *one* good reason why you want to manage
your threads, while java can do it better.
 
M

Matt Humphrey

ByteCoder said:
From your above statements I get the idea you don't really know what
the synchronized keyword does.

The synchronized keyword is used to prevent any other thread calling the
synchronized methods/blocks (that's *all* the synchronized methods in a
class) when one thread is using them. If that one thread is done (the
method completed) the other threads can use the method again on a first
come, first serve basis (they don't 'wait in line'). The thread that's
waiting who 'gets there first' can execute the method.
This all happens automatically. You only have to specify the
synchronized keyword before a (or more) method(s).

Don't forget that the monitor belongs to the object. The same synchronized
method can be called by two different threads if they're being called on
different objects. For static methods the class object is used.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top