diff --git a/jlh/src/main/java/com/jlh/viewer/ArrayListTestList.java b/jlh/src/main/java/com/jlh/viewer/collections/ArrayListTestList.java similarity index 97% rename from jlh/src/main/java/com/jlh/viewer/ArrayListTestList.java rename to jlh/src/main/java/com/jlh/viewer/collections/ArrayListTestList.java index 21c4eafb28b612f1189b3aff49afedb21dbb3a11..b508b3a1e21a0aafa93926587c73e0cb81b80916 100644 --- a/jlh/src/main/java/com/jlh/viewer/ArrayListTestList.java +++ b/jlh/src/main/java/com/jlh/viewer/collections/ArrayListTestList.java @@ -1,4 +1,4 @@ -package com.jlh.viewer; +package com.jlh.viewer.collections; import java.util.ArrayList; diff --git a/jlh/src/main/java/com/jlh/viewer/HashMapTestList.java b/jlh/src/main/java/com/jlh/viewer/collections/HashMapTestList.java similarity index 97% rename from jlh/src/main/java/com/jlh/viewer/HashMapTestList.java rename to jlh/src/main/java/com/jlh/viewer/collections/HashMapTestList.java index 436449fef0b5b91df81a82304860b06587ede2a7..595058218b1728ba3be8f8f51a192bcba8872611 100644 --- a/jlh/src/main/java/com/jlh/viewer/HashMapTestList.java +++ b/jlh/src/main/java/com/jlh/viewer/collections/HashMapTestList.java @@ -1,4 +1,4 @@ -package com.jlh.viewer; +package com.jlh.viewer.collections; import java.util.HashMap; diff --git a/jlh/src/main/java/com/jlh/viewer/HashSetTestList.java b/jlh/src/main/java/com/jlh/viewer/collections/HashSetTestList.java similarity index 85% rename from jlh/src/main/java/com/jlh/viewer/HashSetTestList.java rename to jlh/src/main/java/com/jlh/viewer/collections/HashSetTestList.java index 5a706d970e8986ccb8c4b95b4be5550ac3332ae2..07bd27d695a121e94a739154c8b9df0bc0df7331 100644 --- a/jlh/src/main/java/com/jlh/viewer/HashSetTestList.java +++ b/jlh/src/main/java/com/jlh/viewer/collections/HashSetTestList.java @@ -1,4 +1,4 @@ -package com.jlh.viewer; +package com.jlh.viewer.collections; import java.util.HashSet; @@ -14,14 +14,14 @@ public class HashSetTestList { // 因为hashmap找bucket的时候需要hash 然后找到bucket的时候找entry需要equal 所以两者都要 hashSet.add(new Tuple("jlh","123")); hashSet.add(new Tuple("jlh","123")); - hashSet.stream().forEach(System.out::println); + hashSet.forEach(System.out::println); } public static class Tuple{ private String name; private String address; - public Tuple(String name, String address) { + Tuple(String name, String address) { this.name = name; this.address = address; } @@ -33,8 +33,7 @@ public class HashSetTestList { Tuple tuple = (Tuple) o; - if (name != null ? !name.equals(tuple.name) : tuple.name != null) return false; - return address != null ? address.equals(tuple.address) : tuple.address == null; + return (name != null ? name.equals(tuple.name) : tuple.name == null) && (address != null ? address.equals(tuple.address) : tuple.address == null); } @Override diff --git a/jlh/src/main/java/com/jlh/viewer/thread/SynchronziedTestList.java b/jlh/src/main/java/com/jlh/viewer/thread/SynchronziedTestList.java new file mode 100644 index 0000000000000000000000000000000000000000..56e88f126776711d46ff679058e90da2ad261ddf --- /dev/null +++ b/jlh/src/main/java/com/jlh/viewer/thread/SynchronziedTestList.java @@ -0,0 +1,70 @@ +package com.jlh.viewer.thread; + +import java.time.LocalDateTime; +import java.util.concurrent.TimeUnit; + +/** + * Created by jlh + * On 2017/3/10 0010. + */ +public class SynchronziedTestList { + public static class ClassA{ + public synchronized void methodA() throws InterruptedException { + for (int i=0;i<10;i++) { + System.out.println(Thread.currentThread().getName()+"methodA" + LocalDateTime.now()); + TimeUnit.SECONDS.sleep(1); + } + } + public synchronized void methodB () throws InterruptedException { + for (int i=0;i<10;i++) { + System.out.println(Thread.currentThread().getName()+"methodB" + LocalDateTime.now()); + TimeUnit.SECONDS.sleep(1); + } + } + public static synchronized void methodC() throws InterruptedException { + for (int i=0;i<10;i++) { + System.out.println(Thread.currentThread().getName()+"methodC" + LocalDateTime.now()); + TimeUnit.SECONDS.sleep(1); + } + } + + public static void main(String[] args) throws InterruptedException { + //Synchronized 锁在方法上的时候相当于是Synchronized (this) 所以同一个对象的两个方法不能同一时间访问 + // 会阻塞,对于不同对象则不阻塞, 监视对象的概念 + ClassA classA = new ClassA(); + //与其他轮流输出,因为锁对象是类,和this又不一样,即监视器不同 + new Thread(()->{ + try { + ClassA.methodC(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + new Thread(()->{ + try { + classA.methodA(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + new Thread(()->{ + try { + classA.methodB(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + //A或者B一方先输出完,才能让另一方输出 + ClassA classA1 = new ClassA(); + new Thread(()->{ + try { + classA1.methodA(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + //与前两者无关独自输出 因为对象不同所以 synchronized this 也不同,即监视器不同 + TimeUnit.SECONDS.sleep(20); + } + } +}