Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads access to private instance variables  RSS feed

 
Sanjay Virkar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was expecting following code to produce different output. Currently
I am getting output as.
=Output==============================================================
I am John I am working on : 1
I am Jill I am working on : 1
I am Dan I am working on : 1
I am John I am working on : 2
I am Jill I am working on : 2
I am Dan I am working on : 2
I am John I am working on : 3
I am Jill I am working on : 3
I am Dan I am working on : 3

=Expected Output========================================================
I am John I am working on : 1
I am Jill I am working on : 2
I am Dan I am working on : 3

========================================================================
If each thread has access to private instance variable then when one thread changes the value the other thread should see the change (is what I thought.)
I do get that behavior when I define 'count' as static variable but I don't want to define variable as static.

=Code ===================================================================
import java.util.*;

class worker implements Runnable
{
/* This class simply runs a thread to count to 3 */
private int count = 0;

public void run()
{
while (count < 3)
{
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
System.out.println("Exception occured");
}

synchronized(this)
{
dowork();
}
}
}

private synchronized void dowork()
{
this.count++;
System.out.println(" I am " + Thread.currentThread().getName() + " I am working on : " + this.count );
}

public static void main(String args[])
{
worker w1 = new worker();
worker w2 = new worker();
worker w3 = new worker();

Thread t1 = new Thread(w1);
t1.setName("John");

Thread t2 = new Thread(w2);
t2.setName("Jill");

Thread t3 = new Thread(w3);
t3.setName("Dan");

t1.start();
t2.start();
t3.start();
}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with threads or access control ("private") -- it's just understanding how objects work. Each worker object has its own variable named "count". If you want all three threads to share the same variable, then they should all be using the same worker object.
 
Sanjay Virkar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I should have passed same 'worker' object to all three threads. I modified the code and now I do see that threads do see each other's changes. Thanks!

=Output==================================================================
I am Jill I am working on : 1
I am John I am working on : 2
I am Dan I am working on : 3
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!