Thursday 2 March 2017

Chapter 30 Exercise 10, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

30.10 (Use synchronized sets) Using synchronization, correct the problem
in the preceding exercise so that the second thread does not throw a
ConcurrentModificationException.


import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Exercise10 {

 public static Set<Integer> set = Collections.synchronizedSet(new HashSet<Integer>());

 public static void main(String[] args) {
  new Thread1();
  new Thread2();
 }

 static class Thread1 implements Runnable {

  public Thread1() {
   Thread thread = new Thread(this);
   thread.start();
  }

  @Override
  public void run() {
   for (int i = 0; i < 10000000; i++) {
    set.add(i);
   }
  }
 }
 
 static class Thread2 implements Runnable {

  public Thread2() {
   Thread thread = new Thread(this);
   thread.start();
  }

  @Override
  public void run() {
   try {
    while (true) {
     System.out.println(set.size());
     synchronized (set) {
      for (Iterator<Integer> iterator = set.iterator(); iterator.hasNext();) {
       iterator.next();      
      }      
     }
     Thread.sleep(1000);
    }
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
}

No comments :

Post a Comment