Created
September 3, 2022 13:36
-
-
Save isicju/d60c11cdf53671f795d6f6903571ddb2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.demo.multi; | |
| import java.util.*; | |
| import java.util.concurrent.CopyOnWriteArrayList; | |
| import java.util.concurrent.atomic.AtomicInteger; | |
| import java.util.concurrent.atomic.AtomicReference; | |
| import java.util.concurrent.locks.ReadWriteLock; | |
| import java.util.concurrent.locks.ReentrantLock; | |
| import java.util.concurrent.locks.ReentrantReadWriteLock; | |
| public class Main { | |
| static class SomeObject { | |
| int value = 10; | |
| public SomeObject() { | |
| } | |
| } | |
| static Queue<String> queue = new LinkedList<>(); | |
| static Producer producer = new Producer(); | |
| static Producer producer1 = new Producer(); | |
| static Consumer consumer = new Consumer(); | |
| static Consumer consumer1 = new Consumer(); | |
| private final static Object lock = new Object(); | |
| private static AtomicInteger atomicInteger = new AtomicInteger(10); | |
| private static AtomicReference<Object> atomicObject = new AtomicReference<>(10); | |
| private Map<String, String> map = new HashMap<>(); | |
| static ReentrantLock reentrantLock = new ReentrantLock(); | |
| static ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); | |
| private List<String> list = new CopyOnWriteArrayList<>(); | |
| public static void main(String[] args) { | |
| producer.setName("producerHighPriority"); | |
| producer1.setName("producer1"); | |
| consumer.setName("consumer"); | |
| consumer1.setName("consumer1"); | |
| producer.start(); | |
| producer1.start(); | |
| consumer.start(); | |
| consumer1.start(); | |
| } | |
| static void addValueLock() { | |
| try { | |
| readWriteLock.writeLock().lock(); | |
| } finally { | |
| readWriteLock.writeLock().unlock(); | |
| } | |
| } | |
| static void readValueReentrant() throws InterruptedException { | |
| try { | |
| readWriteLock.readLock().lock(); | |
| } finally { | |
| readWriteLock.readLock().unlock(); | |
| } | |
| } | |
| static class Producer extends Thread { | |
| Producer() { | |
| super(() -> { | |
| try { | |
| addValue(); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| }); | |
| } | |
| } | |
| static class Consumer extends Thread { | |
| Consumer() { | |
| super(() -> { | |
| try { | |
| readValue(); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| }); | |
| } | |
| } | |
| static void addValue() throws InterruptedException { | |
| while (true) { | |
| synchronized (lock) { | |
| if (queue.size() > 10) { | |
| System.out.println("more than 10 I fell asleep: " + Thread.currentThread().getName()); | |
| lock.notifyAll(); | |
| lock.wait(); | |
| } else { | |
| System.out.println("less than 10 I produce: " + Thread.currentThread().getName()); | |
| queue.add(Thread.currentThread().getName() + " " + System.nanoTime()); | |
| } | |
| } | |
| } | |
| } | |
| static void readValue() throws InterruptedException { | |
| while (true) { | |
| synchronized (lock) { | |
| if (queue.size() > 10) { | |
| System.out.println("WTF" + queue.size()); | |
| } | |
| if (queue.isEmpty()) { | |
| lock.notifyAll(); | |
| lock.wait(); | |
| } else { | |
| System.out.println(Thread.currentThread().getName() + " reading: " + queue.poll() + " " + System.nanoTime()); | |
| lock.notifyAll(); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment