diff --git a/03concurrency/0301/src/main/java/java0/conc0301/ThreadCount.java b/03concurrency/0301/src/main/java/java0/conc0301/ThreadCount.java index 8e13af9e..0f4ac38e 100644 --- a/03concurrency/0301/src/main/java/java0/conc0301/ThreadCount.java +++ b/03concurrency/0301/src/main/java/java0/conc0301/ThreadCount.java @@ -3,9 +3,9 @@ public class ThreadCount { public static void main(String[] args) throws InterruptedException { //System.out.println("system:"+Thread.currentThread().getThreadGroup().getParent()); - Thread.currentThread().getThreadGroup().getParent().list(); +// Thread.currentThread().getThreadGroup().getParent().list(); // System.out.println("main:"+Thread.currentThread().getThreadGroup()); -// Thread.currentThread().getThreadGroup().list(); + Thread.currentThread().getThreadGroup().list(); } } diff --git a/03concurrency/0301/src/main/java/java0/conc0301/base/FinalTest.java b/03concurrency/0301/src/main/java/java0/conc0301/base/FinalTest.java new file mode 100644 index 00000000..908a0f70 --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0301/base/FinalTest.java @@ -0,0 +1,14 @@ +package java0.conc0301.base; + +/** + * @Desc + * @Author wfy + * @Date 2021/2/1 14:19 + */ +public class FinalTest { + final int a; + + public FinalTest() { + a = 2; + } +} diff --git a/03concurrency/0301/src/main/java/java0/conc0301/base/IntStreamTest.java b/03concurrency/0301/src/main/java/java0/conc0301/base/IntStreamTest.java new file mode 100644 index 00000000..9d1b5a70 --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0301/base/IntStreamTest.java @@ -0,0 +1,13 @@ +package java0.conc0301.base; + +/** + * @Desc + * @Author wfy + * @Date 2021/2/1 14:14 + */ +public class IntStreamTest { + public static void main(String[] args) { + int loopNum = 100_0000; + + } +} diff --git a/03concurrency/0301/src/main/java/java0/conc0301/base/InterruptedTest.java b/03concurrency/0301/src/main/java/java0/conc0301/base/InterruptedTest.java new file mode 100644 index 00000000..c3b4cd74 --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0301/base/InterruptedTest.java @@ -0,0 +1,27 @@ +package java0.conc0301.base; + +import java.util.concurrent.TimeUnit; + +/** + * @Desc + * @Author wfy + * @Date 2021/1/26 13:32 + */ +public class InterruptedTest { + + public static void main(String[] args) { + Thread t1 = new Thread(() -> { + System.out.println("run:" + Thread.currentThread().getName() + Thread.currentThread().getId()); + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e) { + throw new RuntimeException(); + } + Thread.interrupted(); + System.out.println("alive!"); + }); + t1.start(); + t1.interrupt(); + System.out.println("111"); + } +} diff --git a/03concurrency/0301/src/main/java/java0/conc0301/base/PossibleReordering.java b/03concurrency/0301/src/main/java/java0/conc0301/base/PossibleReordering.java new file mode 100644 index 00000000..70c3a68f --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0301/base/PossibleReordering.java @@ -0,0 +1,27 @@ +package java0.conc0301.base; + +public class PossibleReordering { + static int x = 0, y = 0; + static int a = 0, b = 0; + + public static void main(String[] args) throws InterruptedException { + Thread one = new Thread(new Runnable() { + public void run() { + a = 1; + x = b; + } + }); + + Thread other = new Thread(new Runnable() { + public void run() { + b = 1; + y = a; + } + }); + one.start(); + other.start(); + one.join(); + other.join(); + System.out.println(" (" + x + "," + y + ")"); + } +} \ No newline at end of file diff --git a/03concurrency/0301/src/main/java/java0/conc0301/op/Join.java b/03concurrency/0301/src/main/java/java0/conc0301/op/Join.java index 1cbf6a5a..86337cb3 100644 --- a/03concurrency/0301/src/main/java/java0/conc0301/op/Join.java +++ b/03concurrency/0301/src/main/java/java0/conc0301/op/Join.java @@ -10,12 +10,12 @@ public static void main(String[] args) { thread1.setOo(oo); thread1.start(); - synchronized (oo) { // 这里用oo或thread1/this + synchronized (thread1) { // 这里用oo或thread1/this for (int i = 0; i < 100; i++) { if (i == 20) { try { - oo.wait(0); - //thread1.join(); +// oo.wait(0); + thread1.join(); } catch (InterruptedException e) { e.printStackTrace(); } @@ -42,7 +42,7 @@ public MyThread(String name) { @Override public void run() { - synchronized (oo) { // 这里用oo或this,效果不同 + synchronized (this) { // 这里用oo或this,效果不同 for (int i = 0; i < 100; i++) { System.out.println(name + i); } diff --git a/03concurrency/0301/src/main/java/java0/conc0301/op/WaitAndNotify01.java b/03concurrency/0301/src/main/java/java0/conc0301/op/WaitAndNotify01.java new file mode 100644 index 00000000..a8b55b4f --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0301/op/WaitAndNotify01.java @@ -0,0 +1,58 @@ +package java0.conc0301.op; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +public class WaitAndNotify01 { + + public static void main(String[] args) { + Queue q = new Queue(); + Thread t1 = new Thread(q::consume); + Thread t2 = new Thread(q::produce); + t1.start(); + t2.start(); + } + + + private static class Queue { + private AtomicInteger count = new AtomicInteger(0); + + private static final int SIZE = 10; + + public void consume() { + while (true) { + synchronized (this) { + System.out.println("consume " + count.get()); + if (count.get() <= 0) { + System.out.println("队列为空。。"); + try { + wait(); + } catch (InterruptedException e) { + } + } else { + count.decrementAndGet(); + } + notifyAll(); + } + } + } + + public void produce() { + while (true) { + synchronized (this) { + System.out.println("produce " + count.get()); + if (count.get() >= SIZE) { + System.out.println("队列满了"); + try { + wait(); + } catch (InterruptedException e) { + } + } else { + count.incrementAndGet(); + } + notifyAll(); + } + } + } + } +} diff --git a/03concurrency/0301/src/main/java/java0/conc0301/op/WaitTest.java b/03concurrency/0301/src/main/java/java0/conc0301/op/WaitTest.java new file mode 100644 index 00000000..cee92cd6 --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0301/op/WaitTest.java @@ -0,0 +1,44 @@ +package java0.conc0301.op; + +import java.util.concurrent.TimeUnit; + +/** + * @Desc + * @Author wfy + * @Date 2021/1/25 15:12 + */ +public class WaitTest { + public static void main(String[] args) throws InterruptedException { + Thread t = new Thread(() -> { + while (true) { + synchronized (WaitTest.class) { + System.out.println(1); + try { + TimeUnit.SECONDS.sleep(1); + WaitTest.class.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + + System.out.println(3); + } + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }); + t.start(); + TimeUnit.SECONDS.sleep(1); + while (true) { + synchronized (WaitTest.class) { + System.out.println(2); + WaitTest.class.notify(); + } + TimeUnit.SECONDS.sleep(1); + } + + } +} diff --git a/03concurrency/0301/src/main/java/java0/conc0301/pool/ThreadPoolTest.java b/03concurrency/0301/src/main/java/java0/conc0301/pool/ThreadPoolTest.java new file mode 100644 index 00000000..7e6f13f7 --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0301/pool/ThreadPoolTest.java @@ -0,0 +1,19 @@ +package java0.conc0301.pool; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * @program: 0301 + * @description: + * @author: wfy + * @create: 2021-01-26 23:33 + */ +public class ThreadPoolTest { + public static void main(String[] args) { + TimeUnit unit; + BlockingQueue workQueue; +// ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, , unit, workQueue); + } +} diff --git a/03concurrency/0301/src/main/java/java0/conc0301/sync/Counter.java b/03concurrency/0301/src/main/java/java0/conc0301/sync/Counter.java index 68d869f6..25cfc6b1 100644 --- a/03concurrency/0301/src/main/java/java0/conc0301/sync/Counter.java +++ b/03concurrency/0301/src/main/java/java0/conc0301/sync/Counter.java @@ -38,6 +38,8 @@ public static void main(String[] args) throws InterruptedException { }); t1.start(); t2.start(); +// t1.join(); +// t2.join(); Thread.sleep(1000); // while (Thread.activeCount()>2){//当前线程的线程组中的数量>2 // Thread.yield(); diff --git a/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ConditionTest.java b/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ConditionTest.java new file mode 100644 index 00000000..93cd47a0 --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ConditionTest.java @@ -0,0 +1,71 @@ +package java0.conc0302.threadpool; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @Desc + * @Author wfy + * @Date 2021/2/4 10:38 + */ +public class ConditionTest { + static Lock lock = new ReentrantLock(true); + static Condition fullCondition = lock.newCondition(); + static Condition emptyCondition = lock.newCondition(); + static int size = 0; + static int capacity = 10; + + //空了才入队。 满了才出队。 + public static void main(String[] args) { + enqueue(); + dequeue(); + } + + public static void enqueue() { + new Thread(() -> { + lock.lock(); + try { + while (true) { + if (size < capacity) { + TimeUnit.MILLISECONDS.sleep(100); + ++size; + System.out.println("enqueue " + size); + } else { + emptyCondition.signalAll(); + fullCondition.await(); + } + } + } catch (Exception e) { + + } finally { + lock.unlock(); + } + + }).start(); + } + + public static void dequeue() { + new Thread(() -> { + lock.lock(); + try { + while (true) { + if (size > 0) { + TimeUnit.MILLISECONDS.sleep(100); + --size; + System.out.println("dequeue " + size); + } else { + fullCondition.signalAll(); + emptyCondition.await(); + } + } + } catch (Exception e) { + + } finally { + lock.unlock(); + } + + }).start(); + } +} diff --git a/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ExceptionDemo.java b/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ExceptionDemo.java index c9174072..766e70ee 100644 --- a/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ExceptionDemo.java +++ b/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ExceptionDemo.java @@ -7,20 +7,20 @@ public class ExceptionDemo { public static void main(String[] args) { - ExecutorService executorService = Executors.newFixedThreadPool(1); + ExecutorService executorService = Executors.newFixedThreadPool(2); - try { - Future future = executorService.submit(() -> { - throw new RuntimeException("executorService.submit()"); - }); - - double b = future.get(); - System.out.println(b); - - } catch (Exception ex) { - System.out.println("catch submit"); - ex.printStackTrace(); - } +// try { +// Future future = executorService.submit(() -> { +// throw new RuntimeException("executorService.submit()"); +// }); +// +// double b = future.get(); +// System.out.println(b); +// +// } catch (Exception ex) { +// System.out.println("catch submit"); +// ex.printStackTrace(); +// } try { executorService.execute(() -> { diff --git a/03concurrency/0301/src/main/java/java0/conc0302/threadpool/Mutex.java b/03concurrency/0301/src/main/java/java0/conc0302/threadpool/Mutex.java new file mode 100644 index 00000000..d30594cb --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0302/threadpool/Mutex.java @@ -0,0 +1,82 @@ +package java0.conc0302.threadpool; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.AbstractQueuedSynchronizer; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; + +class Mutex implements Lock { + // 静态内部类,自定义同步器 + private static class Sync extends AbstractQueuedSynchronizer { + // 是否处于占用状态 + @Override + protected boolean isHeldExclusively() { + return getState() == 1; + } + + // 当状态为0的时候获取锁 + @Override + public boolean tryAcquire(int acquires) { + if (compareAndSetState(0, 1)) { + setExclusiveOwnerThread(Thread.currentThread()); + return true; + } + return false; + } + + // 释放锁,将状态设置为0 + @Override + protected boolean tryRelease(int releases) { + if (getState() == 0) throw new IllegalMonitorStateException(); + setExclusiveOwnerThread(null); + setState(0); + return true; + } + + // 返回一个Condition,每个condition都包含了一个condition队列 + Condition newCondition() { + return new ConditionObject(); + } + } + + // 仅需要将操作代理到Sync上即可 + private final Sync sync = new Sync(); + + @Override + public void lock() { + sync.acquire(1); + } + + @Override + public boolean tryLock() { + return sync.tryAcquire(1); + } + + @Override + public void unlock() { + sync.release(1); + } + + @Override + public Condition newCondition() { + return sync.newCondition(); + } + + public boolean isLocked() { + return sync.isHeldExclusively(); + } + + public boolean hasQueuedThreads() { + return sync.hasQueuedThreads(); + } + + @Override + public void lockInterruptibly() throws InterruptedException { + sync.acquireInterruptibly(1); + } + + @Override + public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { + return sync.tryAcquireNanos(1, unit.toNanos(timeout)); + } +} \ No newline at end of file diff --git a/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ReentrantLockTest.java b/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ReentrantLockTest.java new file mode 100644 index 00000000..989f6b66 --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ReentrantLockTest.java @@ -0,0 +1,53 @@ +package java0.conc0302.threadpool; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @Desc + * @Author wfy + * @Date 2021/2/3 18:05 + */ +public class ReentrantLockTest { + static Lock lock = new ReentrantLock(true); + static int count = 0; + + public static void main(String[] args) { + Thread interT = null; + for (int i = 0; i < 2; i++) { + Thread t = new Thread(ReentrantLockTest::incr); + t.start(); + if (i == 1) { + interT = t; + } + } +// try { +// TimeUnit.SECONDS.sleep(1); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + } + + public static void incr() { + boolean locked = false; + try { + locked = lock.tryLock(10, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + System.out.println("error"); + e.printStackTrace(); + } + if (locked) { + Condition c = lock.newCondition(); + try { + for (int i = 0; i < 100000; i++) { + count++; + System.out.println(count); + } + } finally { + lock.unlock(); + } + } + } +} \ No newline at end of file diff --git a/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ReentrantLockTest01.java b/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ReentrantLockTest01.java new file mode 100644 index 00000000..ef0bccb6 --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0302/threadpool/ReentrantLockTest01.java @@ -0,0 +1,43 @@ +package java0.conc0302.threadpool; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @Desc + * @Author wfy + * @Date 2021/2/3 18:05 + */ +public class ReentrantLockTest01 { + static ReentrantLock lock = new ReentrantLock(true); + static int count = 0; + + public static void main(String[] args) { + for (int i = 0; i < 1; i++) { + Thread t = new Thread(ReentrantLockTest01::incr); + t.start(); + } +// try { +// TimeUnit.SECONDS.sleep(1); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + } + + public static void incr() { + System.out.println(lock.getHoldCount()); + lock.lock(); + lock.lock(); + lock.lock(); + try { + System.out.println(lock.getHoldCount()); + } finally { + lock.unlock(); + lock.unlock(); + lock.unlock(); + } + System.out.println(lock.getHoldCount()); + } +} \ No newline at end of file