-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
submit java-advanced-ru/sync-primitives
- Loading branch information
1 parent
b973d87
commit a60f224
Showing
5 changed files
with
75 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"assignment":"java-advanced-ru/multithreading-java"} | ||
{"assignment":"java-advanced-ru/sync-primitives"} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.gradle/ | ||
build/ | ||
caches/ | ||
.idea/ | ||
.gradle/ | ||
build/ | ||
caches/ | ||
.idea/ |
44 changes: 22 additions & 22 deletions
44
java-advanced-ru/sync-primitives/src/main/java/exercise/App.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
package exercise; | ||
|
||
class App { | ||
|
||
public static void main(String[] args) { | ||
// BEGIN | ||
var list = new SafetyList(); | ||
var thread1 = new ListThread(list); | ||
var thread2 = new ListThread(list); | ||
thread1.start(); | ||
thread2.start(); | ||
try { | ||
thread1.join(); | ||
thread2.join(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
System.out.println(list.getSize()); | ||
// END | ||
} | ||
} | ||
|
||
package exercise; | ||
|
||
class App { | ||
|
||
public static void main(String[] args) { | ||
// BEGIN | ||
var list = new SafetyList(); | ||
var thread1 = new ListThread(list); | ||
var thread2 = new ListThread(list); | ||
thread1.start(); | ||
thread2.start(); | ||
try { | ||
thread1.join(); | ||
thread2.join(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
System.out.println(list.getSize()); | ||
// END | ||
} | ||
} | ||
44 changes: 22 additions & 22 deletions
44
java-advanced-ru/sync-primitives/src/main/java/exercise/ListThread.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
package exercise; | ||
|
||
// BEGIN | ||
public class ListThread extends Thread { | ||
SafetyList list; | ||
public ListThread(SafetyList list) { | ||
this.list = list; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
for (int i = 0; i < 1000; i++){ | ||
try { | ||
Thread.sleep(1); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
list.add((int) (Math.random() * 1000)); | ||
} | ||
} | ||
} | ||
// END | ||
package exercise; | ||
|
||
// BEGIN | ||
public class ListThread extends Thread { | ||
SafetyList list; | ||
public ListThread(SafetyList list) { | ||
this.list = list; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
for (int i = 0; i < 1000; i++){ | ||
try { | ||
Thread.sleep(1); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
list.add((int) (Math.random() * 1000)); | ||
} | ||
} | ||
} | ||
// END |
52 changes: 26 additions & 26 deletions
52
java-advanced-ru/sync-primitives/src/main/java/exercise/SafetyList.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
package exercise; | ||
|
||
class SafetyList { | ||
// BEGIN | ||
int[] array; | ||
int size; | ||
|
||
SafetyList() { | ||
array = new int[2100]; | ||
size = 0; | ||
} | ||
|
||
synchronized void add(int number) { | ||
array[size] = number; | ||
size++; | ||
} | ||
|
||
public int get(int index) { | ||
return index > size ? 0 : array[index]; | ||
} | ||
|
||
public int getSize() { | ||
return this.size; | ||
} | ||
// END | ||
} | ||
package exercise; | ||
|
||
class SafetyList { | ||
// BEGIN | ||
int[] array; | ||
int size; | ||
|
||
SafetyList() { | ||
array = new int[2100]; | ||
size = 0; | ||
} | ||
|
||
synchronized void add(int number) { | ||
array[size] = number; | ||
size++; | ||
} | ||
|
||
public int get(int index) { | ||
return index > size ? 0 : array[index]; | ||
} | ||
|
||
public int getSize() { | ||
return this.size; | ||
} | ||
// END | ||
} |