Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > thread problem - any ideas here?

Reply
Thread Tools

thread problem - any ideas here?

 
 
Jamie
Guest
Posts: n/a
 
      03-15-2005
Hi there,

I have several threads (2..n) that need to execute a block of code
(for discussion sake, call this "A"), and only one thread (1) that
needs to execute a block of code "B". Now, threads (2..n) must be able
to execute A simultaneously, however, they may not do so while thread
(1) is executing block of code "B". Thread (1) can only execute block
of code "B" when threads (2..n) are not executing block of code "A".
The precise number of threads is unknown. How does one achieve this in
Java? The below code will not work because the synchronized(mutex)
statement will mean that threads (1..n) can execute block of code A
simultaneously. Right?

class Example
{
Object mutex;

class Threads2toN implements Runnable
{
public run()
{
synchronized(mutex) {
//block of code A;
}

}
}

class Thread1
{
public run() {
synchronized(mutex) {
// block of code B;
}
}
}
 
Reply With Quote
 
 
 
 
Ferenc Hechler
Guest
Posts: n/a
 
      03-15-2005
Hi Jamie,

you can use a counter, how many threads are currently executing block A.
the counter may be locked by Thread2ToN only for short, when it is increased
or decreased.
Thread1 locks the counter the whole time while processing block B, so no
other thread
can increase the counter while executing block B.

Thread2ToN may send a notify for any waiting Therad1 if counter reaches 0.

The code might look something like that:

class Example
{
Object mutex;
int counter = 0;

class Threads2toN implements Runnable
{
public run()
{
synchronized(mutex) {
counter += 1;
}
//block of code A;
synchronized(mutex) {
counter -= 1;
if (counter == 0) {
mutex.notify();
}
}
}
}

class Thread1
{
public run() {
synchronized(mutex) {
while (count > 0) {
mutex.wait();
}
// block of code B;
}
}
}


"Jamie" <> schrieb im Newsbeitrag
news: om...
> Hi there,
>
> I have several threads (2..n) that need to execute a block of code
> (for discussion sake, call this "A"), and only one thread (1) that
> needs to execute a block of code "B". Now, threads (2..n) must be able
> to execute A simultaneously, however, they may not do so while thread
> (1) is executing block of code "B". Thread (1) can only execute block
> of code "B" when threads (2..n) are not executing block of code "A".
> The precise number of threads is unknown. How does one achieve this in
> Java? The below code will not work because the synchronized(mutex)
> statement will mean that threads (1..n) can execute block of code A
> simultaneously. Right?
>
> class Example
> {
> Object mutex;
>
> class Threads2toN implements Runnable
> {
> public run()
> {
> synchronized(mutex) {
> //block of code A;
> }
>
> }
> }
>
> class Thread1
> {
> public run() {
> synchronized(mutex) {
> // block of code B;
> }
> }
> }



 
Reply With Quote
 
 
 
 
shakah
Guest
Posts: n/a
 
      03-15-2005
Would a read/write lock (one writer, many readers) abstraction work
here? If B obtains the lock for writing it would block the A threads,
and it would allow multiple A threads to execute otherwise.

Though I don't think Java has a RWLock, googling for "Java read write
mutex" yields a few promising-looking links, e.g.
http://www.asingh.net/technical/rwlocks.html.

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      03-15-2005
shakah wrote:

> Though I don't think Java has a RWLock


They have just been added in 1.5.0 as part of the new concurrency package. For
instance, java.util.concurrent.locks.ReentrantReadWriteLock.

-- chris


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Graphics/Mainboard/Other problem - any ideas please? bch@genie.co.uk Computer Support 6 08-27-2005 07:41 PM
Perplexing problem, any ideas Steve Computer Information 0 11-10-2004 06:49 PM
Any Ideas on Speaker Problem Jenn Computer Support 10 11-14-2003 06:59 AM
Cant view any icons that are saved on my desktop>? any ideas Fokker Computer Support 3 09-20-2003 09:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57