11

Concurrency and interferences

The program ThreadInterferenceTest starts two Threads in parallel. The first, the Maker, increments a variable 1000000 times. The second, the Taker, decrements the same variable 1000000 times too. At the end, the program displays the value of the variable.

  1. public class ThreadInterferenceTest {
  2.     static final private int NCALLS = 1000000;
  3.  
  4.     private int counter = 0;
  5.  
  6.     private Thread maker, taker;
  7.  
  8.     public ThreadInterferenceTest() {
  9.         maker = new Thread(new Maker());
  10.         taker = new Thread(new Taker());
  11.     }
  12.  
  13.     public int getCounter() {
  14.         return counter;
  15.     }
  16.  
  17.     // MUST be synchonized
  18.     public void inc() {
  19.         counter++;
  20.     }
  21.  
  22.     // MUST be synchonized
  23.     public void dec() {
  24.         counter--;
  25.     }
  26.  
  27.     public void go() {
  28.         maker.start();
  29.         taker.start();
  30.         try {
  31.             maker.join();
  32.             taker.join();
  33.         }
  34.         catch (InterruptedException e) {
  35.         }
  36.     }
  37.  
  38.     private class Maker implements Runnable {
  39.         public void run() {
  40.             for (int i = 0; i < NCALLS; i++)
  41.                 inc();
  42.         }
  43.     }
  44.  
  45.     private class Taker implements Runnable {
  46.         public void run() {
  47.             for (int i = 0; i < NCALLS; i++)
  48.                 dec();
  49.         }
  50.     }
  51.  
  52.     public static void main(String args[]) {
  53.         ThreadInterferenceTest test = new ThreadInterferenceTest();
  54.         test.go();
  55.         System.out.println(test.getCounter());
  56.     }
  57. }
$ javac ThreadInterferenceTest.java
$ java ThreadInterferenceTest
-92387
$ java ThreadInterferenceTest
-784617

Instead of obtaining the expected value, 0, the counter displays a random value. NOTE: If you get 0, run the program again. If necessary, increase the number of calls by changing the value of the constant NCALLS.

Add the keyword synchronized before the data type returned by inc and dec, between public and void, to ask Java to make sure that the execution of these methods is never interrupted.

  1.     public synchronized void inc() {
  1.     public synchronized void dec() {
$ javac ThreadInterferenceTest.java
$ java ThreadInterferenceTest
0

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].