Re: help on threads/monitor needed
From: ByteCoder (ByteCoder_at_127.0.0.1)
Date: 12/30/04
- Next message: ByteCoder: "Re: How to place control in JList?"
- Previous message: for_leska_at_mail15.com: "How to place control in JList?"
- In reply to: frank.jaeger_at_wagner-bremen.de: "Re: help on threads/monitor needed"
- Next in thread: frank.jaeger_at_wagner-bremen.de: "Re: help on threads/monitor needed"
- Reply: frank.jaeger_at_wagner-bremen.de: "Re: help on threads/monitor needed"
- Reply: Matt Humphrey: "Re: help on threads/monitor needed"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 30 Dec 2004 17:59:30 +0100
frank.jaeger@wagner-bremen.de wrote:
> 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.
--
-------------
- ByteCoder - ...I see stupid people
-------------
Curiosity *Skilled* the cat
- Next message: ByteCoder: "Re: How to place control in JList?"
- Previous message: for_leska_at_mail15.com: "How to place control in JList?"
- In reply to: frank.jaeger_at_wagner-bremen.de: "Re: help on threads/monitor needed"
- Next in thread: frank.jaeger_at_wagner-bremen.de: "Re: help on threads/monitor needed"
- Reply: frank.jaeger_at_wagner-bremen.de: "Re: help on threads/monitor needed"
- Reply: Matt Humphrey: "Re: help on threads/monitor needed"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]