diff --git a/.gitignore b/.gitignore index f133c83..52910e0 100644 --- a/.gitignore +++ b/.gitignore @@ -207,3 +207,4 @@ /Semester 4/Assignments/Multithreading_CalebFontenot/target/ /Semester 4/Assignments/MapASDV_CalebFontenot/nbproject/private/ /Semester 4/Assignments/MapASDV_CalebFontenot/build/ +/Semester 4/Assignments/Hashing_CalebFontenot/target/ diff --git a/Semester 4/Assignments/Hashing_CalebFontenot/pom.xml b/Semester 4/Assignments/Hashing_CalebFontenot/pom.xml new file mode 100644 index 0000000..dddd277 --- /dev/null +++ b/Semester 4/Assignments/Hashing_CalebFontenot/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + edu.slcc.asdv.caleb + Hashing_CalebFontenot + 1.0-SNAPSHOT + jar + + UTF-8 + 20 + 20 + edu.slcc.asdv.caleb.hashing_calebfontenot.Hashing_CalebFontenot + + \ No newline at end of file diff --git a/Semester 4/Assignments/Hashing_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/hashing_calebfontenot/MapASDV.java b/Semester 4/Assignments/Hashing_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/hashing_calebfontenot/MapASDV.java new file mode 100644 index 0000000..8e966ac --- /dev/null +++ b/Semester 4/Assignments/Hashing_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/hashing_calebfontenot/MapASDV.java @@ -0,0 +1,791 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + */ + +package edu.slcc.asdv.caleb.hashing_calebfontenot; + +/** + * + * @author caleb + */ +import asdv.ListASDV; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.BiConsumer;//needed in putAll + +public class MapASDV implements Map, Cloneable +{ + + private int capacity = 4; + private double loadFactor = 0.75; + private ArrayList>> map = new ArrayList>>(); + + private Set sharedKeySet = new SharedSet(); + private Set> sharedEntrySet = new SharedSet>(); + private Collection sharedValuesCollection = new SharedCollection(); + + private class SharedCollection extends ArrayList + { + + public boolean addValue(V v) + { + return this.add(v); + } + + @Override + public boolean remove(Object o) + { + + //> The parameter is key not entry if we are here + //>>remove value) and key) from the map + return MapASDV.this.removeFirstValue(o); + + //>>remove key from shared values set + //return super.remove(o); + } + + /** + * Removes one value from the collection. This method is meant to be + * called from out class. The overridden remove(Object o) of this inner + * class, calls the remove of the outer class(MapASDV), and the remove + * of the outer class, calls remove(V v, boolean callFromOuterClass) + * instead of remove(Object o) to avoid Stack Overflow when remover of + * inner calls remove of outer and vice versa. + * + * @param o - the key + * @param callFromOuterClass - dummy variable. + * @return true if the key was removed from the Set + */ + public boolean remove(V v, boolean callFromOuterClass) + { + //remove key from shared keys set + boolean b = super.remove(v); + return b; + + } + + @Override + public Object clone() + { + return super.clone(); + } + + @Override + public void clear() + { + super.clear(); + } + } + + private class SharedSet extends HashSet + { + + @Override + public boolean add(E e) + { + throw new UnsupportedOperationException("Not supported...."); + } + + @Override + public boolean removeAll(Collection c) + { + throw new UnsupportedOperationException("Not supported...."); + } + + /** + * Adds an EntryASDV to the set. It is private and cannot be used by the + * user of the Set. It is used from the MapASDV to add EntriesASDV to + * the SharedSet. Without this method we wouldn't be able to create a + * Set of keys or a Set of entries to give to the user via methods + * keySet() and entrySet() of this Map + * + * @param e EntryASDV + * @return true if the entry was added false otherwise + */ + private boolean addEntry(E e) + { + return super.add(e); + } + + @Override + public boolean remove(Object o) + { + + //>if parameter oo is EntryASDV call auxiliary method removeEntry + if (o instanceof EntryASDV) + { + return removeEntry((EntryASDV) o); + } + + //> The parameter is key not entry if we are here + //>>remove key from the map + MapASDV.this.remove(o); + //>>remove key from shared keys set + return super.remove(o); + } + + /** + * Removes the entry for the shared set + * + * @param entry the entry to be removed + * @return true if the entry was removed, false otherwise + */ + private boolean removeEntry(EntryASDV entry) + { + + MapASDV.this.remove(entry.getKey()); + return super.remove(entry); + } + + /** + * Removes the key from the set. This method is meant to be called from + * out class. The overridden remove(Object o) of this inner class calls + * the remove of the out class, and the remove of the outer class calls + * remove(K o, boolean callFromOuterClass) instead of remove(Object o) + * to avoid Stack Overflow when remover of inner calls remove of outer + * and vice versa. + * + * @param o - the key + * @param callFromOuterClass - dummy variable. + * @return true if the key was removed from the Set + */ + public boolean remove(E o, boolean callFromOuterClass) + { + + //remove key from shared keys set + return super.remove(o); + } + + @Override + public Object clone() + { + return super.clone(); + } + + @Override + public void clear() + { + super.clear(); + } + + } + + public MapASDV() + { + for (int i = 0; i < capacity; ++i) + { + map.add(new ListASDV>()); + } + } + + /** + * Double the size of the Map and rehashes the entries of the Map + */ + private void doubleTheSizeOfTheMapAndRehash() + { + capacity *= 2; + //>create a new arrayList of ListsASDV + ArrayList>> newMap = new ArrayList>>(); + + //>Add at every enetry of the arrayList a new ASDVList + for (int i = 0; i < capacity; ++i) + { + newMap.add(new ListASDV>()); + } + + //>for the size of the OLD arrayList + for (int i = 0; i < map.size(); ++i)//array list + { + //>> get The ASDVlist at i + ListASDV> list = map.get(i); + + //>>for the size() of the ASDVlist + for (int j = 0; j < list.size(); ++j) + { + ///>>>hash and put in the the new array + int index = hash( list.get(j).getKey().hashCode() ); + newMap.get(index).add(list.get(j)); + + } + } + map = newMap; + } + + class EntryASDV implements Entry, Comparable + { + + K key; + V value; + + public EntryASDV(K key, V value) + { + this.key = key; + this.value = value; + } + + @Override + public K getKey() + { + return key; + } + + @Override + public V getValue() + { + return value; + } + + @Override + public V setValue(V value) + { + V oldValue = this.value; + this.value = value; + return oldValue; + } + + @Override + public String toString() + { + return "EntryASDV{" + "key=" + key + ", value=" + value + '}'; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + if (obj == null) + { + return false; + } + if (getClass() != obj.getClass()) + { + return false; + } + final EntryASDV other = (EntryASDV) obj; + if (!Objects.equals(this.key, other.key)) + { + return false; + } + return true; + } + + /** + * + * @param o + * @return throws IllegalArgumentException if parameter class is not K + */ + @Override + public int compareTo(K o) + { + if (getClass() != o.getClass()) + { + throw new IllegalArgumentException("ellegal parameter " + o); + } + return ((Comparable) key).compareTo(o); + } + + } + + @Override + public int size() + { + return this.keySet().size(); + } + + @Override + public boolean isEmpty() + { + throw new UnsupportedOperationException("Not supported...."); + + } + + @Override + public boolean containsKey(Object key) + { + throw new UnsupportedOperationException("Not supported yet...."); + + } + + /** + * Return an entry of the map + * + * @param key the key of the entry to be returned + * @return the entry, or null if the key is not in the map + */ + private EntryASDV getEntryForKey(Object key) + { + for (ListASDV> list : map) + { + Iterator> it = list.iterator(); + while (it.hasNext()) + { + EntryASDV entry = it.next(); + if (key.equals(entry.getKey())) + { + return entry; + } + } + } + return null; + } + + /** + * Returns the index of the given key + * + * @param key a key of the map + * @return the index of a key in the map or -1, if the key is not in the map + */ + private int getIndexForKey(Object key) + { + for (int i = 0; i < map.size(); ++i) + { + Iterator> it = map.get(i).iterator(); + while (it.hasNext()) + { + EntryASDV entry = it.next(); + if (key.equals(entry.getKey())) + { + return i; + } + } + } + return -1; + } + + /** + * Returns true if this map maps one or more keys to the specified value. + * More formally, returns true if and only if this map contains at least one + * mapping to a value v such that (value==null ? v==null : value.equals(v)). + * This operation will probably require time linear in the map size for most + * implementations of the Map interface. + * + * Parameters: value - value whose presence in this map is to be tested + * Returns: true if this map maps one or more keys to the specified value + * Throws: ClassCastException - if the value is of an inappropriate type for + * this map (optional) NullPointerException - if the specified value is null + * and this map does not permit null values (optional) + * + * @param value - value whose presence in this map is to be tested + * @return true if this map maps one or more keys to the specified value + * @throws NullPointerException - if the specified value is null and this + * map does not permit null values (optional) + */ + @Override + public boolean containsValue(Object value) + { + throw new UnsupportedOperationException("Not supported yet...."); + + } + + /** + * Returns the value to which the specified key is mapped, or null if this + * map contains no mapping for the key. + * + * More formally, if this map contains a mapping from a key k to a value v + * such that (key==null ? k==null : key.equals(k)), then this method returns + * v; otherwise it returns null. (There can be at most one such mapping.) + * + * If this map permits null values, then a return value of null does not + * necessarily indicate that the map contains no mapping for the key; it's + * also possible that the map explicitly maps the key to null. The + * containsKey operation may be used to distinguish these two cases. + * + * @param key - the key whose associated value is to be returned + * @return the value to which the specified key is mapped, or null if this + * map contains no mapping for the key + * @throws NullPointerException - if the specified key is null and this map + * does not permit null keys (optional) + */ + @Override + public V get(Object key) + { + throw new UnsupportedOperationException("Not supported yet...."); + + } + + /** + * Associates the specified value with the specified key in this map + * (optional operation). If the map previously contained a mapping for the + * key, the old value is replaced by the specified value. (A map m is said + * to contain a mapping for a key k if and only if m.containsKey(k) would + * return true.) + * + * @param key - key with which the specified value is to be associated + * @param value - value to be associated with the specified key + * @return the previous value associated with key, or null if there was no + * mapping for key. (A null return can also indicate that the map previously + * associated null with key, if the implementation supports null values.) + * @throws NullPointerException - if specified key or value is null and this + * map does not permit null keys + */ + @Override + public V put(K key, V value) + { + + if (key == null || value == null) + { + throw new NullPointerException("parm(s) null"); + } + + //>if contains the key, replace the key's value + EntryASDV entry = getEntryForKey(key); + if (entry != null) + { + V oldValue = entry.value; + entry.value = value; + + return oldValue; + } + ///>>hash and put in the array + int code = this.hash(key.hashCode()); + int index = hash(code); + + ListASDV> list = map.get(index); + EntryASDV e = new EntryASDV(key, value); + list.add(e); + + //>>add the key to the shared keys-set + ((SharedSet) this.sharedKeySet).addEntry(key); + ((SharedSet>) this.sharedEntrySet).addEntry(e); + + //>>get the value of this entry + V v = list.get(list.size() - 1).getValue(); + + //>> add value to the value collection + ((SharedCollection) this.sharedValuesCollection).addValue(v); + + //>> if reach 75% capacity double the size + if ((double) this.size() / capacity >= 0.75) + { + this.doubleTheSizeOfTheMapAndRehash(); + } + + //>>return the value of Entry just added + return v; + } + + int hash(int keyHashCode) + { + int h = hashHash(keyHashCode); + return Math.abs(h % capacity - 1); + } + + /** + * Removes the first entry with the given values. + * + * @param value - the value to be removed + * @return true if removed, false otherwise + * @throws NullPointerException if the value is null + */ + private boolean removeFirstValue(Object value) + { + throw new UnsupportedOperationException("Not supported yet...."); + + } + + /** + * Ensure hash code is evenly distributed + * + * @param h - hash code + * @return evenly distributed hash code + */ + private static int hashHash(int h) + { + h ^= (h >>> 20) ^ (h >>> 12); + return h ^ (h >>> 7) ^ (h >>> 4); + } + + /** + * Removes the mapping for a key from this map if it is present (optional + * operation). More formally, if this map contains a mapping from key k to + * value v such that (key==null ? k==null : key.equals(k)), that mapping is + * removed. (The map can contain at most one such mapping.) + * + * @param key - key whose mapping is to be removed from the map + * @return the previous value associated with key, or null if there was no + * mapping for key. + * @throws NullPointerException - if the specified key is null and this map + * does not permit null keys + */ + @Override + public V remove(Object key) + { + throw new UnsupportedOperationException("Not supported yet...."); + + } + + /** + * Copies all of the mappings from the specified map to this map (optional + * operation). The effect of this call is equivalent to that of calling + * put(k, v) on this map once for each mapping from key k to value v in the + * specified map. The behavior of this operation is undefined if the + * specified map is modified while the operation is in progress. + * + * @param m - mappings to be stored in this map + * @throws NullPointerException - if the specified map is null, or if this + * map does not permit null keys or values, and the specified map contains + * null keys + */ + @Override + public void putAll(Map m) + { + if (m == null) + { + throw new NullPointerException("null parameter"); + } + + BiConsumer consumeEachEntry = new BiConsumer() + { + MapASDV mapForConsumer = MapASDV.this; + + @Override + public void accept(K k, V v) + { + mapForConsumer.put(k, v); + } + }; + + m.forEach(consumeEachEntry); + } + + + /** + * Removes all of the mappings from this map (optional operation). The map + * will be empty after this call returns. Any shared sets are also cleared. + */ + @Override + public void clear() + { + throw new UnsupportedOperationException("Not supported yet...."); + + } + + /** + * Returns a Set view of the keys contained in this map. The set is backed + * by the map, so changes to the map are reflected in the set, and + * vice-versa. If the map is modified while an iteration over the set is in + * progress (except through the iterator's own remove operation), the + * results of the iteration are undefined. The set supports element removal, + * which removes the corresponding mapping from the map, via the + * Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. + * It does not support the add or addAll operations. + * + * @return a set view of the keys contained in this map + */ + @Override + public Set keySet() + { + + return this.sharedKeySet; + } + + /** + * Returns a Collection view of the values contained in this map. The + * collection is backed by the map, so changes to the map are reflected in + * the collection, and vice-versa. If the map is modified while an iteration + * over the collection is in progress (except through the iterator's own + * remove operation), the results of the iteration are undefined. The + * collection supports element removal, which removes the corresponding + * mapping from the map, via the Iterator.remove, Collection.remove, + * removeAll, retainAll and clear operations. It does not support the add or + * addAll operations. + * + * @return + */ + @Override + public Collection values() + { + return sharedValuesCollection; + } + + /** + * Returns a Set view of the mappings contained in this map. The set is + * backed by the map, so changes to the map are reflected in the set, and + * vice-versa. If the map is modified while an iteration over the set is in + * progress (except through the iterator's own remove operation, or through + * the setValue operation on a map entry returned by the iterator) the + * results of the iteration are undefined. The set supports element removal, + * which removes the corresponding mapping from the map, via the + * Iterator.remove, Set.remove, removeAll, retainAll and clear operations. + * It does not support the add or addAll operations. + * + * + * @return a set view of the mappings contained in this map + */ + @Override + public Set> entrySet() + { + return this.sharedEntrySet; + } + + @Override + public String toString() + { + String s = "[ "; + for (int i = 0; i < capacity; ++i) + { + s += map.get(i).toString() + "\n"; + } + s += " ]"; + + return s; + } + + /** + * Created a deep copy of the MapASDV + * + * @return the deep copy of the map + */ + @Override + public Object clone() + { + /* + MapASDV clonedMap = new MapASDV(); + + //clonedMap.putAll(this); + + for (ListASDV< EntryASDV> list : this.map) + { + ListASDV< EntryASDV> l = (ListASDV< EntryASDV>) list.clone(); + clonedMap.map.add(l); + } + return clonedMap; + */ + + MapASDV clonedMap = new MapASDV(); + clonedMap.putAll(this); + return clonedMap; + } + + public static void main(String[] args) + { + + MapASDV map = new MapASDV(); + System.out.println("---------------------------testing put(K, V)"); + map.put("ann", 20); + map.put("coco", 25); + System.out.println(map); + mapASDV.MapASDV clonedMap = ( mapASDV.MapASDV) map.clone(); + + System.out.println("\n\n---------------------------testing double-the-size-and-rehash by reaching 0.75 load factor with another put(K, V)"); + map.put("Jonathan", 30); + System.out.println(map); + map.put("Jonhathan", 45); + System.out.println(map); + map.put("Alexander", 33); + System.out.println(map); + + System.out.println("\n\n---------------------------testing putAll(Map)"); + Map anotherJavaMap = new HashMap(); + anotherJavaMap.put("lion king", 45); + anotherJavaMap.put("HYENA", 6); + map.putAll(anotherJavaMap); + System.out.println(map); + + + System.out.println("\n\n---------------------------testing containsKey"); + System.out.println(map.containsKey("Alexander")); + System.out.println(map.containsKey("alexander")); + + System.out.println("\n\n---------------------------testing containsValue"); + System.out.println(map.containsValue(33)); + System.out.println(map.containsValue(34)); + + System.out.println("\n\n---------------------------testing getEntryForKey"); + Entry e = map.getEntryForKey("Alexander"); + System.out.println( map.getEntryForKey("Alexander")); + System.out.println( map.getEntryForKey("Alex")); + + System.out.println("\n\n---------------------------testing get"); + System.out.println(map.get( "Alexander") ); + System.out.println(map.get( "Alex") ); + try{ map.get( null);}catch (NullPointerException ex ){System.out.println(ex.getMessage());} + + System.out.println("\n\n---------------------------testing getIndexForKey"); + System.out.println(map.getIndexForKey("Alexander")); + System.out.println(map.getIndexForKey("Alex")); + + System.out.println("\n\n---------------------------testing isEmpty"); + System.out.println( map.isEmpty()); + + + System.out.println("\n\n---------------------------testing size"); + System.out.println( map.size()); + System.out.println( map); + + System.out.println("\n\n---------------------------testing entrySet()"); + Set> entries = map.entrySet(); + System.out.println( entries); + + System.out.println("\n\n---------------------------testing keySet()"); + Set keys = map.keySet(); + System.out.println( keys ); + + System.out.println("\n\n---------------------------testing values()"); + Collection values = map.values(); + System.out.println( values); + + System.out.println("\n\n---------------------------testing remove( K) coco 25"); + map.remove("coco"); + System.out.println(map); + System.out.println(entries); + System.out.println(keys); + System.out.println(values); + + System.out.println("\n\n---------------------------testing Entry-Collection remove "); + entries.remove( e); + System.out.println(map); + System.out.println(entries); + System.out.println(keys); + System.out.println(values); + + + System.out.println("\n\n---------------------------testing Set Keys remove "); + keys.remove( "ann"); + System.out.println(map); + System.out.println(entries); + System.out.println(keys); + System.out.println(values); + + + System.out.println("\n\n---------------------------testing Set Values remove "); + values.remove( 45); + System.out.println(map); + System.out.println(entries); + System.out.println(keys); + System.out.println(values); + + System.out.println("\n\n---------------------------testing clear "); + map.clear(); + System.out.println(map); + System.out.println(entries); + System.out.println(keys); + + System.out.println("\n\n---------------------------testing add of sets and collections "); + try{ keys.add( "a");}catch (Exception ex ){System.out.println(ex.getMessage());} + try{ values.add( 33);}catch (Exception ex ){System.out.println(ex.getMessage());} + try{ entries.add( e);}catch (Exception ex ){System.out.println(ex.getMessage());} + + + + System.out.println("\n\n---------------------------testing clone"); + System.out.println( clonedMap); + + System.out.println("---------------------------testing put(K, V) AGAIN"); + map.put("Nicholas", 100); + map.put("a", 200); + map.put("b", -20); + System.out.println( map); + + } + +} diff --git a/Semester 4/Assignments/Threads_CalebFontenot/labThreads2.pdf b/Semester 4/Assignments/Threads_CalebFontenot/labThreads2.pdf new file mode 100644 index 0000000..7d36a7f Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/labThreads2.pdf differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/pom.xml b/Semester 4/Assignments/Threads_CalebFontenot/pom.xml new file mode 100644 index 0000000..7fb8c2f --- /dev/null +++ b/Semester 4/Assignments/Threads_CalebFontenot/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + edu.slcc.asdv.caleb + Threads_CalebFontenot + 1.0-SNAPSHOT + jar + + UTF-8 + 20 + 20 + edu.slcc.asdv.caleb.threads_calebfontenot.Threads_CalebFontenot + + \ No newline at end of file diff --git a/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/threads_calebfontenot/Threads_CalebFontenot.java b/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/threads_calebfontenot/Threads_CalebFontenot.java new file mode 100644 index 0000000..dd961be --- /dev/null +++ b/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/threads_calebfontenot/Threads_CalebFontenot.java @@ -0,0 +1,16 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + */ + +package edu.slcc.asdv.caleb.threads_calebfontenot; + +/** + * + * @author caleb + */ +public class Threads_CalebFontenot { + + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountNoSync.java b/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountNoSync.java new file mode 100644 index 0000000..75aba54 --- /dev/null +++ b/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountNoSync.java @@ -0,0 +1,62 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package sync; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * + * @author caleb + */ +public class AccountNoSync { + + private static Account account = new Account(); + + public static void main(String[] args) + { + ExecutorService executor = Executors.newCachedThreadPool(); + // Create and launch 100 threads + for (int i = 0; i < 100; ++i) { + executor.execute(new AddAPennyTask()); + } + executor.shutdown(); + while (!executor.isTerminated()); + System.out.println("Balance is " + account.getBalance()); + } + + // This is a thread for adding a penny to the account + private static class AddAPennyTask implements Runnable { + + public void run() + { + account.deposit(1); + } + } + // An inner class for account + private static class Account { + + private int balance = 0; + + private int getBalance() + { + return balance; + } + + public void deposit(int amount) + { + int newBalance = balance + amount; + + // This delay is deliberately added to magnify the data corruption and makes it easy to see... + //try { + //Thread.sleep(3); + //} catch (InterruptedException ex) { + + //} + balance = newBalance; + } + } + +} diff --git a/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSync2.java b/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSync2.java new file mode 100644 index 0000000..65e480e --- /dev/null +++ b/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSync2.java @@ -0,0 +1,64 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package sync; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * + * @author caleb + */ +public class AccountSync2 { + + private static Account account = new Account(); + + public static void main(String[] args) + { + ExecutorService executor = Executors.newCachedThreadPool(); + // Create and launch 100 threads + for (int i = 0; i < 100; ++i) { + executor.execute(new AddAPennyTask()); + } + executor.shutdown(); + while (!executor.isTerminated()); + System.out.println("Balance is " + account.getBalance()); + } + + // This is a thread for adding a penny to the account + private static class AddAPennyTask implements Runnable { + + public void run() + { + account.deposit(1); + } + } + // An inner class for account + private static class Account { + + private int balance = 0; + + private int getBalance() + { + return balance; + } + + public void deposit(int amount) + { + + int newBalance = balance + amount; + + // This delay is deliberately added to magnify the data corruption and makes it easy to see... + try { + Thread.sleep(3); + } catch (InterruptedException ex) { + + } + balance = newBalance; + + } + } + +} diff --git a/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSync3.java b/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSync3.java new file mode 100644 index 0000000..8f743fc --- /dev/null +++ b/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSync3.java @@ -0,0 +1,70 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package sync; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * + * @author caleb + */ +public class AccountSync3 { + + private static Lock lock = new ReentrantLock(); // Create a lock + private static Account account = new Account(); + + public static void main(String[] args) + { + ExecutorService executor = Executors.newCachedThreadPool(); + // Create and launch 100 threads + for (int i = 0; i < 100; ++i) { + executor.execute(new AddAPennyTask()); + } + executor.shutdown(); + while (!executor.isTerminated()); + System.out.println("Balance is " + account.getBalance()); + } + + // This is a thread for adding a penny to the account + private static class AddAPennyTask implements Runnable { + + public void run() + { + account.deposit(1); + } + } + // An inner class for account + private static class Account { + + private int balance = 0; + + private int getBalance() + { + return balance; + } + + public void deposit(int amount) + { + lock.lock(); // Acquire the lock + try { + int newBalance = balance + amount; + + // This delay is deliberately added to magnify the data corruption and makes it easy to see... + //try { + //Thread.sleep(3); + //} catch (InterruptedException ex) { + + //} + balance = newBalance; + } finally { + lock.unlock(); + } + } + } + +} diff --git a/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSyncronized.java b/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSyncronized.java new file mode 100644 index 0000000..fb4b594 --- /dev/null +++ b/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSyncronized.java @@ -0,0 +1,62 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package sync; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * + * @author caleb + */ +public class AccountSyncronized { + + private static Account account = new Account(); + + public static void main(String[] args) + { + ExecutorService executor = Executors.newCachedThreadPool(); + // Create and launch 100 threads + for (int i = 0; i < 100; ++i) { + executor.execute(new AddAPennyTask()); + } + executor.shutdown(); + while (!executor.isTerminated()); + System.out.println("Balance is " + account.getBalance()); + } + + // This is a thread for adding a penny to the account + private static class AddAPennyTask implements Runnable { + + public void run() + { + account.deposit(1); + } + } + // An inner class for account + private static class Account { + + private int balance = 0; + + private int getBalance() + { + return balance; + } + + public synchronized void deposit(int amount) + { + int newBalance = balance + amount; + + // This delay is deliberately added to magnify the data corruption and makes it easy to see... + try { + Thread.sleep(3); + } catch (InterruptedException ex) { + + } + balance = newBalance; + } + } + +} diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/edu/slcc/asdv/caleb/threads_calebfontenot/Threads_CalebFontenot.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/edu/slcc/asdv/caleb/threads_calebfontenot/Threads_CalebFontenot.class new file mode 100644 index 0000000..0ae0162 Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/edu/slcc/asdv/caleb/threads_calebfontenot/Threads_CalebFontenot.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountNoSync$Account.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountNoSync$Account.class new file mode 100644 index 0000000..da9d3e0 Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountNoSync$Account.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountNoSync$AddAPennyTask.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountNoSync$AddAPennyTask.class new file mode 100644 index 0000000..2398bed Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountNoSync$AddAPennyTask.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountNoSync.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountNoSync.class new file mode 100644 index 0000000..8e718c2 Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountNoSync.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync2$Account.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync2$Account.class new file mode 100644 index 0000000..963fa0e Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync2$Account.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync2$AddAPennyTask.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync2$AddAPennyTask.class new file mode 100644 index 0000000..ab1caec Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync2$AddAPennyTask.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync2.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync2.class new file mode 100644 index 0000000..d9dbf73 Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync2.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync3$Account.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync3$Account.class new file mode 100644 index 0000000..ace874a Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync3$Account.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync3$AddAPennyTask.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync3$AddAPennyTask.class new file mode 100644 index 0000000..dc2e33f Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync3$AddAPennyTask.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync3.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync3.class new file mode 100644 index 0000000..be2f304 Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSync3.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSyncronized$Account.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSyncronized$Account.class new file mode 100644 index 0000000..80d841e Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSyncronized$Account.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSyncronized$AddAPennyTask.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSyncronized$AddAPennyTask.class new file mode 100644 index 0000000..f84f8b8 Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSyncronized$AddAPennyTask.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSyncronized.class b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSyncronized.class new file mode 100644 index 0000000..4a5058f Binary files /dev/null and b/Semester 4/Assignments/Threads_CalebFontenot/target/classes/sync/AccountSyncronized.class differ diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/Semester 4/Assignments/Threads_CalebFontenot/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..112b557 --- /dev/null +++ b/Semester 4/Assignments/Threads_CalebFontenot/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,13 @@ +edu/slcc/asdv/caleb/threads_calebfontenot/Threads_CalebFontenot.class +sync/AccountSync3$Account.class +sync/AccountSync2.class +sync/AccountSync2$AddAPennyTask.class +sync/AccountSync2$Account.class +sync/AccountSyncronized$AddAPennyTask.class +sync/AccountSync3$AddAPennyTask.class +sync/AccountSync3.class +sync/AccountNoSync.class +sync/AccountSyncronized.class +sync/AccountSyncronized$Account.class +sync/AccountNoSync$Account.class +sync/AccountNoSync$AddAPennyTask.class diff --git a/Semester 4/Assignments/Threads_CalebFontenot/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/Semester 4/Assignments/Threads_CalebFontenot/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..1ef5232 --- /dev/null +++ b/Semester 4/Assignments/Threads_CalebFontenot/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,5 @@ +/home/caleb/ASDV-Java/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSync2.java +/home/caleb/ASDV-Java/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/threads_calebfontenot/Threads_CalebFontenot.java +/home/caleb/ASDV-Java/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSyncronized.java +/home/caleb/ASDV-Java/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountSync3.java +/home/caleb/ASDV-Java/Semester 4/Assignments/Threads_CalebFontenot/src/main/java/sync/AccountNoSync.java