From 15b0668c8ff35ad243385c5c6eff5843f87434a0 Mon Sep 17 00:00:00 2001 From: jlhde123 <472327024@qq.com> Date: Fri, 10 Mar 2017 17:23:29 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E9=9B=86=E5=90=88=E7=B1=BB=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0=E7=BB=93=E6=9E=84=E6=9B=B4=E6=96=B0=202=E3=80=81?= =?UTF-8?q?=E6=96=B0=E5=A2=9Esynchronzied=20=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => collections}/ArrayListTestList.java | 2 +- .../{ => collections}/HashMapTestList.java | 2 +- .../{ => collections}/HashSetTestList.java | 9 ++- .../viewer/thread/SynchronziedTestList.java | 70 +++++++++++++++++++ 4 files changed, 76 insertions(+), 7 deletions(-) rename jlh/src/main/java/com/jlh/viewer/{ => collections}/ArrayListTestList.java (97%) rename jlh/src/main/java/com/jlh/viewer/{ => collections}/HashMapTestList.java (97%) rename jlh/src/main/java/com/jlh/viewer/{ => collections}/HashSetTestList.java (85%) create mode 100644 jlh/src/main/java/com/jlh/viewer/thread/SynchronziedTestList.java 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 21c4eaf..b508b3a 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 436449f..5950582 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 5a706d9..07bd27d 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 0000000..56e88f1 --- /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); + } + } +} -- Gitee