Skip to content

Instantly share code, notes, and snippets.

@evaldosantos
Created October 14, 2017 14:07
Show Gist options
  • Save evaldosantos/f9cb72e098461272070945441db76c73 to your computer and use it in GitHub Desktop.
Save evaldosantos/f9cb72e098461272070945441db76c73 to your computer and use it in GitHub Desktop.
Two threads trying to read a property of an object
/*
* 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();
}
}
@evaldosantos
Copy link
Author

evaldosantos commented Oct 14, 2017

/*
 * 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 {
    public static int number = 0;    
}

public class JavaApplication3 {
    
    
    public static void main(String[] args) {
        Thread a = new Thread() {
            public void run() {
                while (true) {
                    if(Monitor.number % 2 == 0) {
                        System.out.println(this.getName() + " - " + Monitor.number);
                        Monitor.number++;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(JavaApplication3.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                
            }
        };
        a.setName("Thread A");
        
        Thread b = new Thread() {
            public void run() {
                while (true) {
                    if(Monitor.number % 2 == 1) {
                        System.out.println(this.getName() + " - " + Monitor.number);
                        Monitor.number++;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(JavaApplication3.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        };
        b.setName("Thread B");
        
        a.start();
        b.start();
    }
    
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment