Skip to content

Commit

Permalink
submit java-advanced-ru/sync-primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
hexlet-cli committed Feb 21, 2024
1 parent b973d87 commit a60f224
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .current.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"assignment":"java-advanced-ru/multithreading-java"}
{"assignment":"java-advanced-ru/sync-primitives"}
8 changes: 4 additions & 4 deletions java-advanced-ru/sync-primitives/.gitignore
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 java-advanced-ru/sync-primitives/src/main/java/exercise/App.java
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
}
}
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
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
}

0 comments on commit a60f224

Please sign in to comment.