thread related questions

P

puzzlecracker

a.

let's say thread A calls wait or sleep during the execution, meanwhile
thread B calls ThreadA.interrupt();

does interrupt method blocks, waiting for A to finish wait/sleep and
then interrupting it? Does it asynchronouysly let jvm know to cancel
thread A upon the return, and those returns immediately?


b. does it make sense (is it allowed to pass runnable class with
synchronized method to Thread class or have one in subclass of Thread,
thereby letting start spawn the thread)to synchronize the run method.
If so, is it a good practice?


thanks
 
E

EJP

puzzlecracker said:
does interrupt method blocks, waiting for A to finish wait/sleep and
then interrupting it?

No, it interrupts it immediately.
Does it asynchronouysly let jvm know to cancel
thread A upon the return, and those returns immediately?

It returns immediately. I have no idea whether the operation is
synchronous or asynchronous, or whether this is even specified, but I
doubt that it matters.
b. does it make sense (is it allowed to pass runnable class with
synchronized method to Thread class or have one in subclass of Thread,
thereby letting start spawn the thread)to synchronize the run method.
If so, is it a good practice?

I don't understand this question.
 
M

Mark Space

EJP said:
No, it interrupts it immediately.


It returns immediately. I have no idea whether the operation is
synchronous or asynchronous, or whether this is even specified, but I
doubt that it matters.

Kinda a good point. I assume that interrupt() is basically a Unix
signal. That is, an interrupt is a little flag that gets set in the
process control block for that process, and then the kernel dispatches
appropriately.

This is pretty easy to envision if there's only one CPU. When the
kernel (or JVM) is setting the interrupt flag, all process are waiting
in some queue, and the kernel is the only thing executing. With
multiple CPUs, there's the issue that ThreadA may be under execution
when some other process tries to interrupt it.

What happens I think has to be architecture dependent. These are
software interrupts, not hardware interrupts, and unless there is some
way in hardware CPU-B can slap CPU-A and tell it to stop executing,
there's no way to deliver the interrupt immediately. I guess one has to
assume that the interrupt is asynchronous for both threads, and it's
only processed when the JVM actually gets around to it.
 
H

hiwa

puzzlecracker ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
a.

let's say thread A calls wait or sleep during the execution, meanwhile
thread B calls ThreadA.interrupt();

does interrupt method blocks, waiting for A to finish wait/sleep and
then interrupting it? Does it asynchronouysly let jvm know to cancel
thread A upon the return, and those returns immediately?


b. does it make sense (is it allowed to pass runnable class with
synchronized method to Thread class or have one in subclass of Thread,
thereby letting start spawn the thread)to synchronize the run method.
If so, is it a good practice?


thanks
If ThreadA was doing wait() or sleep(),
ThreadA.interrupt() would cause an
InterruptedExcepion thrown. Rest would
depend on what the exception handler did.
The interrupt() method itself always
returns immediately.

For your question b, I don't understand
what your are talking about.
 
P

puzzlecracker

For your question b, I don't understand
what your are talking about.

Is the following allowed and/make sense from design point of view?


Thread thread= new Thread ( new Runnable(){
public void synchronized run(){
// do something
} }
);

or effectively inhereit from the Thread class and provide
implementation for run as synchronize.

thanks
 
E

EJP

puzzlecracker said:
Is the following allowed and/make sense from design point of view?

Yes. (new Thread(Runnable)).
or effectively inhereit from the Thread class and provide
implementation for run as synchronize.

Either will work. The API specifies all this, and yo ucan try it
yourself: why are you asking here?
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top