S
Srubys
hiya
1. Why do all the statements in constructor execute prior to
statement “ new Thread(this).start();” , even if “new
Thread(this).start();” precedes the other statements? For example:
class A {
int i = 100;
A(){
new Thread( this ).start();
i++; // i will have value of 101 before new
thread is
started, even though i++ follows the
statement that creates a new thread
}
2. While a thread is inside a synchronized method, all other threads
that try to call any synchronized methods on the same instance have to
wait.
So if for example process has 3 threads ( A, B and C) and A calls some
synchronized method on object D, and while A is still inside D, B and
C also call method on same object. While A is still inside D, will
context switching still happen, even if B and C won’t do anything due
to waiting on thread A to exit the monitor?
3.After experimenting with a code a bit, I came to conclusion that
when a thread enters a synchronized block, all other threads trying to
enter synchronized block on same instance will have to wait until this
thread exits the synchronized block. So, if we again have threads A,
B and C and if thread A enters object’s monitor via synchronized
block:
synchronized( object ) {
for( int i = 0; I < 1000000; i++ );
}
then A will get ahold of object’s monitor even if no methods are
called on that object inside the synchronized block. And B and C won’t
be able to enter synchronized block until A exits the synchronized
block? Right?
BTW – why does infinite for() loop inside synchronized block cause
compile time error?
4. If we again have threads A, B and C and if thread A enters
object’s monitor and then calls notify(), then a thread that called
wait() on the same object is awakened:
• but what if B and C are trying to get ahold of object’s
monitor, but didn’t call wait()? Will they still get notified
via
notify()?
• if thread A calls wait(); but if then none of the threads that
afterwards enter same object’s monitor ever call notify(),
will A ever get awaken or will it always remain in a wait
state?
thank you
1. Why do all the statements in constructor execute prior to
statement “ new Thread(this).start();” , even if “new
Thread(this).start();” precedes the other statements? For example:
class A {
int i = 100;
A(){
new Thread( this ).start();
i++; // i will have value of 101 before new
thread is
started, even though i++ follows the
statement that creates a new thread
}
2. While a thread is inside a synchronized method, all other threads
that try to call any synchronized methods on the same instance have to
wait.
So if for example process has 3 threads ( A, B and C) and A calls some
synchronized method on object D, and while A is still inside D, B and
C also call method on same object. While A is still inside D, will
context switching still happen, even if B and C won’t do anything due
to waiting on thread A to exit the monitor?
3.After experimenting with a code a bit, I came to conclusion that
when a thread enters a synchronized block, all other threads trying to
enter synchronized block on same instance will have to wait until this
thread exits the synchronized block. So, if we again have threads A,
B and C and if thread A enters object’s monitor via synchronized
block:
synchronized( object ) {
for( int i = 0; I < 1000000; i++ );
}
then A will get ahold of object’s monitor even if no methods are
called on that object inside the synchronized block. And B and C won’t
be able to enter synchronized block until A exits the synchronized
block? Right?
BTW – why does infinite for() loop inside synchronized block cause
compile time error?
4. If we again have threads A, B and C and if thread A enters
object’s monitor and then calls notify(), then a thread that called
wait() on the same object is awakened:
• but what if B and C are trying to get ahold of object’s
monitor, but didn’t call wait()? Will they still get notified
via
notify()?
• if thread A calls wait(); but if then none of the threads that
afterwards enter same object’s monitor ever call notify(),
will A ever get awaken or will it always remain in a wait
state?
thank you