-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson20 #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Lesson20 #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package Lesson20; | ||
|
|
||
| public class MyDeadlock { | ||
|
|
||
| public synchronized void acceptThread(MyDeadlock thread) { | ||
|
|
||
| try { | ||
| Thread.sleep(2000); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| thread.printThread(); | ||
| } | ||
|
|
||
| public synchronized void printThread() { | ||
|
|
||
| System.out.println(Thread.currentThread().getName()); | ||
|
|
||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package Lesson20; | ||
|
|
||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * * Создать метод, который печатает название потока и засыпает на 2 секунды. | ||
| * Запустить одновременно 10 потоков. | ||
| * Реализовать механизм синхронизации, чтобы все потоки выполнились последовательно. | ||
| */ | ||
| public class Task55 { | ||
|
|
||
| public static void main(String[] args) throws InterruptedException { | ||
|
|
||
| Stream.generate(() -> new Thread(Task55::printNameThread)) | ||
| .limit(10) | ||
| .forEach(Thread::start); | ||
| } | ||
|
|
||
| public static synchronized void printNameThread() { | ||
| System.out.println(Thread.currentThread().getName()); | ||
| try { | ||
| Thread.sleep(2000); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package Lesson20; | ||
|
|
||
| /** | ||
| * Создать программу, которая реализует deadlock между тремя потоками. | ||
| */ | ||
| public class Task56{ | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| MyDeadlock myDeadlock = new MyDeadlock(); | ||
| MyDeadlock myDeadlock1 = new MyDeadlock(); | ||
| MyDeadlock myDeadlock2 = new MyDeadlock(); | ||
|
|
||
| new Thread(() -> myDeadlock.acceptThread(myDeadlock1)).start(); | ||
| new Thread(() -> myDeadlock1.acceptThread(myDeadlock2)).start(); | ||
| new Thread(() -> myDeadlock2.acceptThread(myDeadlock)).start(); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package Lesson20; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * Есть 3 производителя и 2 потребителя, все разные потоки и работают все | ||
| * одновременно. Есть очередь с 200 элементами. Производители добавляют | ||
| * случайное число от 1..100, а потребители берут эти числа. Если в очереди | ||
| * элементов >= 100 производители спят, если нет элементов в очереди – потребители спят. | ||
| * Если элементов стало <= 80 производители просыпаются. | ||
| * Все это работает до тех пор, пока обработанных элементов не станет 10000, только потом программа завершается. | ||
| */ | ||
| public class Task57 { | ||
| public static final List<Integer> list = new ArrayList<>(); | ||
|
|
||
| private static int s = 0; | ||
| private static final int limit = 10000; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. как пишем название констант? |
||
|
|
||
| public static void main(String[] args) { | ||
| Stream.generate(() -> new Random().nextInt(100)).limit(200).forEach(list::add); | ||
|
|
||
| new Thread(Task57::startConsumer).start(); | ||
| new Thread(Task57::startConsumer).start(); | ||
|
|
||
| new Thread(Task57::startManufacturer).start(); | ||
| new Thread(Task57::startManufacturer).start(); | ||
| new Thread(Task57::startManufacturer).start(); | ||
|
|
||
| } | ||
|
|
||
| private static void startManufacturer() { | ||
| while (s < limit) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. сделай проще 10000 элементов добавь |
||
| addList(); | ||
| } | ||
| } | ||
|
|
||
| private static void startConsumer() { | ||
| while (s < limit) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 10000 элементов удали |
||
| removeList(); | ||
| } | ||
| } | ||
|
|
||
| private static void removeList() { | ||
| synchronized (list) { | ||
| if (list.size() != 0) { | ||
| list.notify(); | ||
| list.remove(0); | ||
| s++; | ||
|
|
||
| } else { | ||
| try { | ||
| list.wait(); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. после wait ничего не надо делать? |
||
| } | ||
|
|
||
| public static void addList() { | ||
| synchronized (list) { | ||
| if (list.size() >= 100) { | ||
| try { | ||
| list.wait(); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| if (list.size() <= 80) { | ||
| list.notifyAll(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. он в конце идти должен |
||
| list.add(new Random().nextInt(100)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
если у нас есть статическая переменная то жто в 99% случаев константы