| Line 3: |
Line 3: |
| | ===With apt=== | | ===With apt=== |
| | <nowiki>sudo apt-add-repository ppa:webupd8team/java | | <nowiki>sudo apt-add-repository ppa:webupd8team/java |
| − | sudo apt-get update
| + | sudo apt-get update |
| − | sudo apt-get install oracle-java8-installer</nowiki>
| + | sudo apt-get install oracle-java8-installer</nowiki> |
| | | | |
| | Also ensure your JAVA_HOME variable has been set to: | | Also ensure your JAVA_HOME variable has been set to: |
| Line 1,273: |
Line 1,273: |
| | </syntaxhighlight> | | </syntaxhighlight> |
| | | | |
| − | == Concurrency == | + | ==Concurrency== |
| | | | |
| − | === syncrhronized === | + | ===syncrhronized=== |
| | If there are multiple synchronized methods on an instance only one of them can be run by threads at one point in time<syntaxhighlight lang="java"> | | If there are multiple synchronized methods on an instance only one of them can be run by threads at one point in time<syntaxhighlight lang="java"> |
| | public class Counter { | | public class Counter { |
| Line 1,289: |
Line 1,289: |
| | </syntaxhighlight> | | </syntaxhighlight> |
| | | | |
| − | === Locks === | + | ===Locks=== |
| | Solve the multiple synchronized methods on same instance (apart from ReentrantLock thre are TryLock and TryForLock)<syntaxhighlight lang="java"> | | Solve the multiple synchronized methods on same instance (apart from ReentrantLock thre are TryLock and TryForLock)<syntaxhighlight lang="java"> |
| | import java.util.concurrent.locks.ReentrantLock; | | import java.util.concurrent.locks.ReentrantLock; |
| Line 1,318: |
Line 1,318: |
| | </syntaxhighlight> | | </syntaxhighlight> |
| | | | |
| − | === Atomic classes === | + | ===Atomic classes=== |
| | Solves the same problem as locks but for simple operators (AtomicInteger, AtomicBoolean, AtomicIntegerArray, AtomicLong, AtomicBoolean....)<syntaxhighlight lang="java"> | | Solves the same problem as locks but for simple operators (AtomicInteger, AtomicBoolean, AtomicIntegerArray, AtomicLong, AtomicBoolean....)<syntaxhighlight lang="java"> |
| | import java.util.concurrent.atomic.AtomicInteger; | | import java.util.concurrent.atomic.AtomicInteger; |
| Line 1,342: |
Line 1,342: |
| | </syntaxhighlight> | | </syntaxhighlight> |
| | | | |
| − | === Concurrent collections === | + | ===Concurrent collections=== |
| | <syntaxhighlight lang="java"> | | <syntaxhighlight lang="java"> |
| | import java.util.Hashtable; | | import java.util.Hashtable; |
| Line 1,379: |
Line 1,379: |
| | </syntaxhighlight> | | </syntaxhighlight> |
| | | | |
| | + | === CopyOnWriteArrayList === |
| | + | Expensive write, fast concurrent read, synchronized writes, free reads. <syntaxhighlight lang="java"> |
| | + | import java.utils.List; |
| | + | import java.util.concurrent.CopyOnWriteArrayList; |
| | + | |
| | + | public class CopyOnWriteArrayListRunner { |
| | + | List<String> list = new CopyOnWriteArrayList<>(); |
| | + | |
| | + | // Threads -3 |
| | + | list.add("Ant"); |
| | + | list.add("Bat"); |
| | + | list.add("Cat"); |
| | + | |
| | + | // Threads - 10000 |
| | + | for (String element:list){ |
| | + | System.out.println(element); |
| | + | } |
| | + | } |
| | + | </syntaxhighlight><br /> |
| | ==Eclipse shortcuts== | | ==Eclipse shortcuts== |
| | Ctrl + Space → Autocomplete.<br /> | | Ctrl + Space → Autocomplete.<br /> |