When different threads access a static method, objects declared in this method are local or shared in java

When different threads access a static method, are the objects declared in this method local or shared between threads in java?

Also, is it safe to call thread.interrupt()on a thread that is doing i / o?

+5
source share
7 answers

Objects declared inside a method staticare not shared between threads. Objects defined outside the method as staticare common.

So:

private static Object thisIsShared;

public static void myMethod() {
    Object thisIsNotShared = new Object();
}

If you are going to name interrupt()for threads running I / O, you should look at using classes that implement the interfaceInterruptableChannel .

+8

() →

, -. , interrupt() , . . InterruptedException. , , . , , , .

, , - , , .

+2

, , . - , - , , .

, . , , , . .

+1

: static vs non-static, .

, . ( ) .

, ,

http://developer.android.com/reference/java/lang/Thread.html

IO, , interrupt() , ( , ). IO, interrupt() .

+1

thread.interrupt() , -, - .

- , , , ClosedByInterruptException.

java.lang.Thread

+1

When different threads access a static method, are the objects declared in this method local or split between threads in java?

Objects declared in any method are local to the method. It does not matter if the method is static or otherwise.

+1
source

Variables declared in a static or non-stationary method are not shared between different threads.

+1
source

All Articles