Created
October 14, 2017 14:07
-
-
Save evaldosantos/f9cb72e098461272070945441db76c73 to your computer and use it in GitHub Desktop.
Two threads trying to read a property of an object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package javaapplication3; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author evaldo | |
*/ | |
class Monitor { | |
private static int number = 0; | |
public static int getNumber() { | |
return number; | |
} | |
public static void increment() { | |
number++; | |
} | |
} | |
class Even implements Runnable { | |
@Override | |
public void run() { | |
while (true) { | |
System.out.println("Even" + " - " + Monitor.getNumber()); | |
} | |
} | |
} | |
class Odd implements Runnable { | |
@Override | |
public void run() { | |
while (true) { | |
System.out.println("Odd" + " - " + Monitor.getNumber()); | |
} | |
} | |
} | |
public class JavaApplication3 { | |
public static void main(String[] args) { | |
Even e = new Even(); | |
Odd o = new Odd(); | |
o.run(); | |
e.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.