J
Jamie
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;
}
}
}
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;
}
}
}