🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #8596

volatile keyword - doesn't seem to make a change

#8596
From: Yonatan Taub
Date: Sat, 10 Mar 2001 20:43
69 lines
2664 bytes
Hello!

As I understand, threads create a local copy of variables which allows them
to execute faster.
They retrieve the master copy only after exiting or entering a synchronized
block or the thread itself.

Here comes the meaning of volatile which is to say that the variable may
change by other threads so each thread using it, always looks at the master
copy.

I've written a simple program which tried to illustrate the volatile's
essence in life:

public class VolatileTest implements Runnable {

  // Should be volatile to work as expected!!!
  private int value;

  public VolatileTest() {
    value = 10;
  }


  public void run() {

    while(value == 10)
      System.out.println("in run() - value=" + value);

    System.out.println("leaving run()");
  }


  public void changeValue(int newValue) {
    value = newValue;
  }


  public static void main(String[] args) {
    VolatileTest vol = new VolatileTest();
    Thread t = new Thread(vol);

    t.start();

    // Snoozing a little in main

    try { Thread.sleep(2000); }
    catch(InterruptedException x) { System.err.println("sleep() interrupted
in main()"); }

    System.out.println("in main() - about to change the variable value");
    vol.changeValue(99);

    System.out.println("leaving main()");
  }

} // end class VolatileTest


The variable "value" was NOT declared volatile but it acts this way.
If it wasn't volatile, the inner thread should have NOT noticed the change
in variable "value" and would continue to execute indefinitly.
This is not the case however.

Can someone help me with this?

P.S.

Is there a newsgroup about threads somewhere?
Technical Details
Message-ID: <3aaa9320_2@dnews>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone-sf.pbi.net!63.208.208.143!feed2.onemain.com!feed1.onemain.com!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!t-online.de!news1.dtag.de!news.stgl.sel.alcatel.de!news.stgl.sel.alcatel.de!bornews.borland.com!dnews!132.64.13.14

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,c0d5023d960f16ab,start
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-03-10 13:10:07 PST
From: "Yonatan Taub" <yonatanta@bashan.co.il>
Newsgroups: borland.public.jbuilder.java.language
Subject: volatile keyword - doesn't seem to make a change
Date: Sat, 10 Mar 2001 22:43:52 +0200
Lines: 71
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
NNTP-Posting-Host: 132.64.13.14
Message-ID: <3aaa9320_2@dnews>
X-Trace: 10 Mar 2001 12:48:32 -0800, 132.64.13.14
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone-sf.pbi.net!63.208.208.143!feed2.onemain.com!feed1.onemain.com!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!t-online.de!news1.dtag.de!news.stgl.sel.alcatel.de!news.stgl.sel.alcatel.de!bornews.borland.com!dnews!132.64.13.14