From 5836493e8b5ac0ae47e4f5b7fc8ad79efffffb55 Mon Sep 17 00:00:00 2001 From: ym Date: Fri, 14 Jan 2022 19:54:15 +0100 Subject: [PATCH] added deadlocks --- .../javawebinar/basejava/LazySingleton.java | 24 +++++++ .../javawebinar/basejava/MainConcurrency.java | 72 +++++++++++++++++++ src/ru/javawebinar/basejava/MainDeadlock.java | 50 +++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 src/ru/javawebinar/basejava/LazySingleton.java create mode 100644 src/ru/javawebinar/basejava/MainConcurrency.java create mode 100644 src/ru/javawebinar/basejava/MainDeadlock.java diff --git a/src/ru/javawebinar/basejava/LazySingleton.java b/src/ru/javawebinar/basejava/LazySingleton.java new file mode 100644 index 0000000..223ce3f --- /dev/null +++ b/src/ru/javawebinar/basejava/LazySingleton.java @@ -0,0 +1,24 @@ +package ru.javawebinar.basejava; + +public class LazySingleton { + volatile private static LazySingleton INSTANCE; + + private LazySingleton() { + } + + private static class LazySingletonHolder { + private static final LazySingleton INSTANCE = new LazySingleton(); + } + + public static LazySingleton getInstance() { + return LazySingletonHolder.INSTANCE; +// if (INSTANCE == null) { +// synchronized (LazySingleton.class) { +// if (INSTANCE == null) { +// INSTANCE = new LazySingleton(); +// } +// } +// } +// return INSTANCE; + } +} \ No newline at end of file diff --git a/src/ru/javawebinar/basejava/MainConcurrency.java b/src/ru/javawebinar/basejava/MainConcurrency.java new file mode 100644 index 0000000..5c3f347 --- /dev/null +++ b/src/ru/javawebinar/basejava/MainConcurrency.java @@ -0,0 +1,72 @@ +package ru.javawebinar.basejava; + +import java.util.ArrayList; +import java.util.List; + +public class MainConcurrency { + public static final int THREADS_NUMBER = 10000; + private int counter; + private static final Object LOCK = new Object(); + + public static void main(String[] args) throws InterruptedException { + System.out.println(Thread.currentThread().getName()); + + Thread thread0 = new Thread() { + @Override + public void run() { + System.out.println(getName() + ", " + getState()); + throw new IllegalStateException(); + } + }; + thread0.start(); + + new Thread(new Runnable() { + + @Override + public void run() { + System.out.println(Thread.currentThread().getName() + ", " + Thread.currentThread().getState()); + } + + private void inc() { + synchronized (this) { +// counter++; + } + } + + }).start(); + + System.out.println(thread0.getState()); + + final MainConcurrency mainConcurrency = new MainConcurrency(); + List threads = new ArrayList<>(THREADS_NUMBER); + + for (int i = 0; i < THREADS_NUMBER; i++) { + Thread thread = new Thread(() -> { + for (int j = 0; j < 100; j++) { + mainConcurrency.inc(); + } + }); + thread.start(); + threads.add(thread); + } + + threads.forEach(t -> { + try { + t.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + System.out.println(mainConcurrency.counter); + } + + private synchronized void inc() { +// synchronized (this) { +// synchronized (MainConcurrency.class) { + counter++; +// wait(); +// readFile +// ... +// } + } +} diff --git a/src/ru/javawebinar/basejava/MainDeadlock.java b/src/ru/javawebinar/basejava/MainDeadlock.java new file mode 100644 index 0000000..67713c1 --- /dev/null +++ b/src/ru/javawebinar/basejava/MainDeadlock.java @@ -0,0 +1,50 @@ +package ru.javawebinar.basejava; + +public class MainDeadlock { + public static Object object1 = new Object(); + public static Object object2 = new Object(); + + public static void main(String[] args) { + Thread1 thread1 = new Thread1(); + Thread2 thread2 = new Thread2(); + thread1.start(); + thread2.start(); + } + + public static class Thread1 extends Thread { + @Override + public void run() { + synchronized (object1) { + System.out.println("Thread1: Holding object1"); + try { + Thread.sleep(15000); + } catch (InterruptedException e) { + } + System.out.println("Thread 1: Waiting for lock 2..."); + + synchronized (object2) { + System.out.println("Thread 1: Holding lock 1 & 2..."); + } + } + } + } + + + public static class Thread2 extends Thread { + @Override + public void run() { + synchronized (object2) { + System.out.println("Thread2: Holding object2"); + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + } + System.out.println("Thread2: Waiting for a lock2...."); + + synchronized (object1) { + System.out.println("Thread2: Holding lock 1 and 2 ..."); + } + } + } + } +}