Skip to content
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

concurrency code style #73

Merged
merged 1 commit into from
Feb 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
concurrency code style
Fix concurrency article code style
  • Loading branch information
vladthelittleone authored Feb 2, 2020
commit 785ad51bc16ed858dedf29909b32efd9d6ba8b27
67 changes: 34 additions & 33 deletions concurrency/Concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
Создать поток просто - мы должны отнаследоваться от `Thread`:
```java
public class MyThread extends Thread {
@Override
public void run() {/*some work*/}
@Override
public void run() {/*some work*/}
}
```

Expand Down Expand Up @@ -69,8 +69,8 @@ public class ThreadExample extends Thread {
Тут поможет второй вариант создания потока. Это разумеется реализация интерфейса! Наш интерфейс называется `Runnable`.
```java
class Task implements Runnable {
@Override
public void run() {/*some work*/}
@Override
public void run() {/*some work*/}
}
```
Запуск:
Expand All @@ -84,8 +84,8 @@ thread.start();
Т.е выглядело бы это как-то так:
```java
Thread thread = new Thread(new Runnable() {
@Override
public void run() {/*some work*/}
@Override
public void run() {/*some work*/}
});

thread.start();
Expand Down Expand Up @@ -126,8 +126,8 @@ public class RunnableExample implements Runnable {
```java
Timer timer = new Timer();
timer.schedule(new TimeTask {
@Override
public void run() {/*some work*/}
@Override
public void run() {/*some work*/}
}, 60);
```

Expand Down Expand Up @@ -238,12 +238,12 @@ Join заставляет дождаться завершения потока
```java
class MyThread extends Thread {
private volatile boolean stopFlag = false;
@Override
public void run() {
while(!stopFlag) {
/*work here*/
}
}
@Override
public void run() {
while(!stopFlag) {
/*work here*/
}
}
}

//bla bla
Expand Down Expand Up @@ -283,12 +283,12 @@ public class ThreadWithFlag extends Thread {
Примерно также работает и второй случай - это через `Thread.currentThread.isInterrupted())`:
```java
class MyThread extends Thread {
@Override
public void run() {
while(!Thread.currentThread.isInterrupted())) {
/*work here*/
}
}
@Override
public void run() {
while(!Thread.currentThread.isInterrupted())) {
/*work here*/
}
}
}

//bla bla
Expand Down Expand Up @@ -323,10 +323,10 @@ public class ThreadInterruptExample extends Thread {
Если сделать как:
```java
class MyThread extends Thread {
@Override
public void run() {
/*work here*/
}
@Override
public void run() {
/*work here*/
}
}

//bla bla
Expand All @@ -340,7 +340,6 @@ thread.interrupt();
public class ThreadWithoutInterruptCheck extends Thread {
@Override
public void run() {

while (true) {
System.out.println("i still alive");
}
Expand Down Expand Up @@ -511,15 +510,17 @@ public class ThreadPoolExample {
Synchronized можно передать объект для лока тоже.
```java
public class Example {
//synchronized на this
public synchronized void test() {
}
//synchronized на this
public synchronized void test() {
/* work here */
}

//synchronized на объекте
public void test() {
synchronized(obj) {
}
}
//synchronized на объекте
public void test() {
synchronized(obj) {
/* work here */
}
}
}
```

Expand Down