-
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.
- Loading branch information
1 parent
c6b9751
commit f6c196b
Showing
5 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/java/com/randomtasks/multithreading/synchronization/SynchronizerDemo.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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.randomtasks.multithreading.synchronization; | ||
|
||
class SumArray { | ||
private int sum; | ||
synchronized int sumArray(int[] arr) { | ||
sum = 0; | ||
for (int item: | ||
arr) { | ||
sum += item; | ||
System.out.println(sum); | ||
try { | ||
Thread.sleep(20); | ||
} | ||
catch (InterruptedException e) { | ||
System.out.println(e); | ||
} | ||
} | ||
return sum; | ||
} | ||
} | ||
|
||
class MyThread implements Runnable { | ||
static SumArray array; | ||
static int summa; | ||
int[] arr; | ||
MyThread(int[] arr) { | ||
array = new SumArray(); | ||
this.arr = new int[arr.length]; | ||
System.arraycopy(arr, 0, this.arr, 0, arr.length); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
System.out.println(array.sumArray(arr));; | ||
} | ||
} | ||
|
||
public class SynchronizerDemo { | ||
public static void main(String[] args) { | ||
Thread thread1 = new Thread(new MyThread(new int[] {10, 10, 10, 10}), "some thr"); | ||
Thread thread2 = new Thread(new MyThread(new int[] {1, 1, 1, 1, 1}), "second thread"); | ||
thread1.start(); | ||
thread2.start(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...a/com/randomtasks/multithreading/synchronization/threadedCounter/ThreadedCounterDemo.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.randomtasks.multithreading.synchronization.threadedCounter; | ||
|
||
class MyThread implements Runnable { | ||
String name; | ||
static int sum; | ||
MyThread(String name) { | ||
this.name = name; | ||
} | ||
|
||
synchronized static private void counter(String name) { | ||
for (int i = 0; i < 20; i++) { | ||
System.out.print(name); | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
counter(name); | ||
} | ||
} | ||
|
||
public class ThreadedCounterDemo { | ||
public static void main(String[] args) { | ||
for (int i = 0; i < 5; i++) { | ||
Thread thread = new Thread(new MyThread( Integer.toString(i))); | ||
thread.setPriority((i + 1) * 2); | ||
thread.start(); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...main/java/com/randomtasks/multithreading/synchronization/waitAndNotify/WaitAndNotify.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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.randomtasks.multithreading.synchronization.waitAndNotify; | ||
|
||
import com.randomtasks.multithreading.threadImplementsRunnable.MyThreadDemo; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
|
||
class UserInputReader { | ||
static String inp; | ||
synchronized void read() { | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | ||
while (true) { | ||
try { | ||
wait(); | ||
if (inp != null && inp.equals("stop")) { | ||
|
||
notify(); | ||
break; | ||
} | ||
inp = reader.readLine(); | ||
notify(); | ||
} | ||
catch (IOException exception) { | ||
System.out.println("I/O error"); | ||
} | ||
catch (InterruptedException exception) { | ||
System.out.println("Interrupted"); | ||
} | ||
} | ||
} | ||
synchronized void write() { | ||
PrintWriter pw = new PrintWriter(System.out, true); | ||
while (true) { | ||
if (inp == null || inp.equals("")) pw.print("Enter some text: "); | ||
else if (inp.equals("stop")) { | ||
pw.println("Quiting..."); | ||
pw.flush(); | ||
notify(); | ||
break; | ||
} | ||
else pw.print("Previous text is: " + inp + " enter new text: "); | ||
pw.flush(); | ||
notify(); | ||
try { | ||
wait(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
class MyThread implements Runnable { | ||
Thread thread; | ||
boolean read; | ||
UserInputReader uir; | ||
MyThread(String name, UserInputReader uir, boolean read) { | ||
thread = new Thread(this, name); | ||
this.uir = uir; | ||
this.read = read; | ||
} | ||
public static MyThread createAndStart(String name, UserInputReader uir, boolean read) { | ||
MyThread thread = new MyThread(name, uir, read); | ||
thread.thread.start(); | ||
return thread; | ||
} | ||
@Override | ||
public void run() { | ||
if (read) uir.read(); | ||
else uir.write(); | ||
} | ||
} | ||
|
||
public class WaitAndNotify { | ||
public static void main(String[] args) { | ||
UserInputReader uir = new UserInputReader(); | ||
MyThread thread1 = MyThread.createAndStart("reader", uir, true); | ||
MyThread thread2 = MyThread.createAndStart("writer", uir, false); | ||
try { | ||
thread1.thread.join(); | ||
thread2.thread.join(); | ||
} | ||
catch (InterruptedException exception) { | ||
System.out.println("Join error"); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/com/randomtasks/multithreading/threadExtendsThread/ExtendThreadDemo.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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.randomtasks.multithreading.threadExtendsThread; | ||
|
||
class MyThread extends Thread { | ||
Thread thread; | ||
MyThread(String name) { | ||
super(name); | ||
} | ||
|
||
public static Thread createAndStart(String name) { | ||
MyThread tempThread = new MyThread(name); | ||
tempThread.start(); | ||
return tempThread; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
for (int i = 0; i < 10; i++) { | ||
Thread.sleep(200); | ||
System.out.println("Counter: " + (i + 1)); | ||
} | ||
} | ||
catch (InterruptedException exception) { | ||
System.out.println(exception); | ||
} | ||
} | ||
} | ||
|
||
class MySecondThread extends MyThread { | ||
static int sum = 0; | ||
private final int[] arr; | ||
MySecondThread(String name, int[] arr) { | ||
super(name); | ||
this.arr = arr; | ||
} | ||
|
||
synchronized void summer() { | ||
sum = 0; | ||
for (int item: this.arr) { | ||
sum += item; | ||
try { | ||
Thread.sleep(10); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
System.out.println(sum); | ||
} | ||
System.out.println("END THREAD #" + thread.getName()); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
summer(); | ||
} | ||
} | ||
|
||
public class ExtendThreadDemo { | ||
public static void main(String[] args) { | ||
MySecondThread thread1 = new MySecondThread("First", new int[] {10, 10, 10, 10}); | ||
MySecondThread thread2 = new MySecondThread("Second", new int[] {1, 1, 1, 1}); | ||
thread1.start(); | ||
thread2.start(); | ||
try { | ||
thread1.join(); | ||
thread2.join(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/randomtasks/multithreading/threadImplementsRunnable/MyThreadDemo.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.randomtasks.multithreading.threadImplementsRunnable; | ||
|
||
class MyThread implements Runnable { | ||
Thread thr; | ||
|
||
MyThread(String thr) { | ||
this.thr = new Thread(this, thr); | ||
} | ||
|
||
public static MyThread starter(String threadName) { | ||
MyThread innerThread = new MyThread("Some thread"); | ||
innerThread.thr.start(); | ||
return innerThread; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
for (int i = 0; i < 10; i++) { | ||
System.out.println("Count: " + i); | ||
Thread.sleep(200); | ||
} | ||
} | ||
catch (InterruptedException exception) { | ||
System.out.println("Interrupted: " + exception); | ||
} | ||
finally { | ||
System.out.println("Thread " + thr.getName() + " ended."); | ||
} | ||
} | ||
} | ||
|
||
public class MyThreadDemo { | ||
public static void main(String[] args) { | ||
MyThread thread = MyThread.starter("Some thr"); | ||
Thread thread2 = new Thread(thread); | ||
thread2.start(); | ||
} | ||
} |