[go: up one dir, main page]

Skip to content

Commit

Permalink
Added Multithreading classes
Browse files Browse the repository at this point in the history
  • Loading branch information
syndersage committed Jul 20, 2022
1 parent c6b9751 commit f6c196b
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 0 deletions.
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();
}
}
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();
}
}
}
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");
}
}
}
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);
}
}
}
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();
}
}

0 comments on commit f6c196b

Please sign in to comment.