Carl Cordova

Greenhorn
+ Follow
since Feb 02, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Carl Cordova

I am looking for some clarification on static synchronized methods.

Say for example, I have a target runnable named tr created by implementing Runnable. This class name is MyRunner. MyRunner has (besides an overridden run method) two STATIC synchronized methods, methodA and methodB.

In the main of MyTestClass, I have instantiated two thread objects, named a and b, passing tr as the target. I then start thread a & b, one after another.

Am I to understand there is only 1 lock for Class MyRunner?

And if this is true, then would this imply either method methodA or methodB can ever be running at any one time?

Thanks,
I attempted to take this code a step further by overloading the doStuff method and initializing the static wrapper. I was expecting the end value of x to be 10, but it is actually 5. I don't understand what is happening.

Here is the output:

$ java Box6
Value of i in doStuff(Integer) is : 10
Value after doStuff is : 5

Thanks,
From page 207 of K&B, line 2?

class Foo {
static int size = 7;
static void changeIt(int size) {
size = size + 200;
System.out.println("size in changeIt is " + size);
}

public static void main (String [] args) {
Foo f = new Foo();
System.out.println("size = " + size);
changeIt(size);
System.out.println("size after changeIt is " + size);
}
}
Maybe I am tired, but is size on line# 2 a Class variable and not an instance?