Home arrow static arrow Java Programming [Archive] - Is the Class thread safe?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Is the Class thread safe?
This topic has 9 replies on 1 page.

Posts:15
Registered: 12/13/01
Is the Class thread safe?  
Aug 4, 2004 7:24 AM



 
Hi, I have the following code and would like to know if Class3 is actually thread safe.

[code]
public class Class1{
public Class1() {}
public createThread()
{
Class3 c3 = new Class3();
new Class2(c3, 0, 1).start();
new Class2(c3, 2, 3).start();
}
}

public class Class2 extends Thread
{
private Class3 c3 = null;
int v1;
int v2;
public Class2(Class3 c3, int v1, int v2)
{
this.c3 = c3;
this.v1 = v1;
this.v2 = v2;
}

public void run()
{
c3.execute(v1,v2);
}
}

public class Class3
{
private int a;
private int b;
public Class3()
{
return;
}

public void execute(int value1, int value2)
{
this.a = value1;
this.b = value2;
}
}

According to my understanding about the JVM, local variable and method parameters are inherently thread safe because they reside on its own working memory for each thread. Now, the question is what if I pass the same object into two threads as an parameter?
Any help would be appreciated.

 

Posts:5,965
Registered: 5/17/03
Re: Is the Class thread safe?  
Aug 4, 2004 7:40 AM (reply 1 of 9)



 
To be thread safe a method like excecute in C3 must be decalared syncronized.
This ensures that only one C2 thread at the time can run it. Otherwise the variables
can be updated in mumbo jumbo order.
 

Posts:37,103
Registered: 3/30/99
Re: Is the Class thread safe?  
Aug 4, 2004 7:40 AM (reply 2 of 9)



 
No, Class3 is not threadsafe.

If two threads concurrently call execute on the same instance of Class3, then when all is said and done, it may have its a set by one thread and its b by the other.

I don't understand what you're asking about passing it as a parameter. Could you elaborate?
 

Posts:3,258
Registered: 00-08-28
Re: Is the Class thread safe?  
Aug 4, 2004 7:52 AM (reply 3 of 9)



 
Hi, I have the following code and would like to know
if Class3 is actually thread safe.

As Ulrika pointed out its not.


[code]
public class Class1{
public Class1() {}
public createThread()
{
Class3 c3 = new Class3();
new Class2(c3, 0, 1).start();
new Class2(c3, 2, 3).start();
}
}

public class Class2 extends Thread
{
private Class3 c3 = null;
int v1;
int v2;
public Class2(Class3 c3, int v1, int v2)
{
this.c3 = c3;
this.v1 = v1;
this.v2 = v2;
}

public void run()
{
c3.execute(v1,v2);
}
}

public class Class3
{
private int a;
private int b;
public Class3()
{
return;
}

public void execute(int value1, int value2)
{
this.a = value1;
this.b = value2;
}
}

According to my understanding about the JVM, local
variable and method parameters are inherently thread
safe because they reside on its own working memory for


v1 and v2 are not thread safe. If value1 and value2 had been local variables yes in that case it would have been.

each thread. Now, the question is what if I pass the
same object into two threads as an parameter?
Any help would be appreciated.
 

Posts:15
Registered: 12/13/01
Re: Is the Class thread safe?  
Aug 4, 2004 7:52 AM (reply 4 of 9)



 
Like what I said in the first post, method parameter is inherently thread safe, in Class1, c3 is passed as an parameter to Class2 constructor before the thread is started. I simply don't know if c3 is still shared by both threads or a copy of c3 is made into the private working memory of each thread.
 

Posts:19,725
Registered: 9/26/01
Re: Is the Class thread safe?  
Aug 4, 2004 7:57 AM (reply 5 of 9)



 
Like what I said in the first post, method parameter
is inherently thread safe, in Class1, c3 is passed as
an parameter to Class2 constructor before the thread
is started. I simply don't know if c3 is still shared
by both threads or a copy of c3 is made into the
private working memory of each thread.

You're passing the same instance to 2 different threads. Each thread is invoking c3.execute at potentially the same time (on that same instance). No, a "copy" of c3 is not made into each thread.
 

Posts:5,965
Registered: 5/17/03
Re: Is the Class thread safe?  
Aug 4, 2004 8:04 AM (reply 6 of 9)



 
Now, the question is what if I pass the
same object into two threads as an parameter?
Any help would be appreciated.

But this is what happens!

Both Class2 threads hold (in the c3 variable) a pointer to the same Class3 object. This means they can
call the execute method at exactly the same time. To prevent this execute must be declared synchronized.
 

Posts:19,725
Registered: 9/26/01
Re: Is the Class thread safe?  
Aug 4, 2004 8:09 AM (reply 7 of 9)



 
Given that your example Class3 class doesn't actually do anything with the "a" and "b" variables it is modifying, in this example it doesn't matter whether it's synchronized or not. The need for synchronization is about atomicity - about whether it would matter for a and b to be "out of synch", one having a value from one supplier and the other having a value from a different supplier. You're not showing how a and b relate as an atomic unit.
 

Posts:37,103
Registered: 3/30/99
Re: Is the Class thread safe?  
Aug 4, 2004 8:13 AM (reply 8 of 9)



 
Like what I said in the first post, method parameter
is inherently thread safe, in Class1, c3 is passed as
an parameter to Class2 constructor before the thread
is started.

Yes, the parameter is safe. T1 can change the value of its copy and it won't affect T2 and vice versa:
void foo(C3 c3) {    c3 = somethingElse;} 


BUT it's only the local c3 reference itself that's isolated. If I do this
void bar(C3 c3) {    c3.a = somethingElse;    c3.b = yetAnother;} 
then the results will be seen by both threads, and you're back in the situation UJ and I described in the first two responses.

Do you see the fundamental difference between foo() and bar() here?

I simply don't know if c3 is still shared
by both threads or a copy of c3 is made into the
private working memory of each thread.

Yes, c3 is copied. But what is c3? What gets copied? Is c3 an object? Nope. It's a reference. Only the reference gets copied. So two threads have two different references--pointers, essentially---to the same object. That's why foo() and bar() each behave the way they do. Two references, one object. And that bit has nothing to do with multithreading--it's a core part of how Java works, and very important to understand.
 

Posts:15
Registered: 12/13/01
Re: Is the Class thread safe?  
Aug 4, 2004 8:23 AM (reply 9 of 9)



 
I see what you are saying. Thanks for all the help and explanation.
 
This topic has 9 replies on 1 page.