• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Code has me confused

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class MyRun implements Runnable
{
Object Lock = new Object();
int K = 100;

public static void main(String... args)
{
MyRun rt = new MyRun();
Thread t1 = new Thread( rt,"A");
Thread t2 = new Thread(rt);
t1.start();
try { Thread.sleep(50); } catch (Exception e) {}
t2.start();
}

public void run()
{
int k = 100;
synchronized (Lock )
{
if ( Thread.currentThread().getName().equals("A") ) K = k = 200;
System.out.println(k + " " + K);
}
}
}

The output is
200 200
100 200

This has me completely confused. The code is locking on the same object so why is the value of k in the second line NOT 200 as it is for K ???
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This has me completely confused. The code is locking on the same object so why is the value of k in the second line NOT 200 as it is for K ???



The lower-case "k" variable, is a local variable. Every call to the method gets its own copy.

Henry
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Victor, It seems you still have some problem in understanding of concurrency issues.

Every Threads share instance variables not local variables.So for thread t2 if condition does not get true, so small k remains 100 and since capital K is shared so its value got changed by thread t1. That's why you are getting
100 200 for thread t2.
 
Sanjit Kumar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Victor, It seems you still have some problem in understanding of concurrency issues.

Every Threads share instance variables not local variables.So for thread t2 if condition does not get true, so small k remains 100 and since capital K is shared so its value got changed by thread t1. That's why you are getting
100 200 for thread t2.
------------------------------------------------------------------------------
No one is intelligent by birth
 
Goodbye moon men. Hello tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic