Wednesday, 15 June 2011

java - How to update inmediately shared variables between threads -


My exam program asks a string and prints every 2 seconds. I have already read about some Java memory models or do not update their variables immediately on threads main memory.

I have tried unstable and static attributes while synchronizing the code blocks in which the variable line was modified. With waiting () / notice () when the variable was changed and some other how do I specify the line as a reference and not in the form of value? Am I trying to do wrong? Why objects can be completely synchronized when they work as monitors but they can not be while working as pointers?

  Public Class TestSharedVariable {static String line = ""; Public Stable Zero Main (String [] Args) {// Scanner Keyboard to read from Keyboard = New Scanner (System.in); Printer p = new printer (line); P.start (); // reads a row in the variable shared with the keyboard, while (! Line.equals ("quit") {line = keyboard.nextLine (); }}} Class printer thread {personal volatile string line; Public printer (string palabra) {this.line = palabra; } / * Line every 2 seconds * / @ override public wide run (while (line! Axles ("left")) {try {sleep (2000); } Grip (Interrupted predefined e) {e.printStackTrace ();} System.out.println (this.getName () + ":" + line); }}}   

Output:

  thread-0: asdf thread-0: thread-0:    
  • One of the two main issues are: The position of a string variable, such as the characters that are included in it, has been created once, and
  • You are specifying two reference variables in the same object, but your assignment of a version Hoping to change Scope changes assignment of other variables, and not how Java reference variables it.

    In other words, although initially you are setting the line variable of the printer object in the same context as the TestSharedVariable static line variable, by changing the context of the Static Line variable of TestShared Vonable, the printer line Variables will not have any effect on reference assignments. To understand the threading, do not do anything with threading and everything to do. Think about reference variables such as pointer in C or other similar languages. If two variables point to the same object, then a variable is assigned to a different object But there is no effect on the first variables to point out.

    In order for your code to work, both variables must share the context of the same mutable object, and you have to change the state of the changable object, not Reference for this.

    For example,

      import java.util.Scanner; Public Class TestShirewareble {stable volatile MutableObject mutableObject = New MutableObject (); Public Stable Zero Main (String [] Args) {// Scanner Keyboard to read from Keyboard = New Scanner (System.in); Printer P = new printer (mutableObject); New thread (P, "Print thread") Start (); // reads a share from the keyboard in the shared variable, while (! MutableObject.getData (.) Equals ("leave")) {mutableObject.setData (keyboard.nextLine ()); }}} Class Printer executes Runnable {Personal Volatile MutableObject mutableObject; Public printer (MutableObject mutableObject) {this.mutableObject = mutableObject; } / * Over 2 seconds * / / Override public is run () (while! MutableObject.getData (.) Equals ("left")) {try {Thread.sleep (2000);} Hold (Interrupted e) {E.printStackTrace ();} Thread Thread = Thread.WrenchTrade (); System.out.println (thread.getName () + ":" + variable object);}}} class MutableObject {Private string data = ""; Public string getData () {return data;} public zeros set data (string data) {this.data = data;} @ override public string to string () {return data;}}    

No comments:

Post a Comment