Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When different threads access a static method, are objects declared in that method local or shared in java [closed]

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

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

like image 747
Sam Adamsh Avatar asked Dec 06 '22 15:12

Sam Adamsh


1 Answers

Objects declared inside a static method are not shared between threads. Objects defined outside the method as static are shared.

So:

private static Object thisIsShared;

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

If you're going to be calling interrupt() on threads doing I/O you should look at using classes that implement the InterruptableChannel interface.

like image 143
Dave Webb Avatar answered Dec 09 '22 05:12

Dave Webb