From 82697b7b350c87f368c467f82804c183646f690e Mon Sep 17 00:00:00 2001 From: shaked Date: Wed, 10 Oct 2018 13:46:34 +0300 Subject: [PATCH] Added more strings exercises as well as one more advanced OOP one --- src/Mainer.java | 6 +++- src/loops/test.md | 3 ++ src/threads/sample/sample1/Mainer.java | 32 ++++++++++++++++++++ src/threads/sample/sample1/MyThread.java | 28 ++++++++++++++++++ src/threads/sample/sample2/Mainer.java | 34 ++++++++++++++++++++++ src/threads/sample/sample2/MyThread.java | 21 +++++++++++++ src/threads/sample/sample2/SharedData.java | 14 +++++++++ src/threads/sample/sample3/Shared.java | 7 +++++ 8 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 src/loops/test.md create mode 100644 src/threads/sample/sample1/Mainer.java create mode 100644 src/threads/sample/sample1/MyThread.java create mode 100644 src/threads/sample/sample2/Mainer.java create mode 100644 src/threads/sample/sample2/MyThread.java create mode 100644 src/threads/sample/sample2/SharedData.java create mode 100644 src/threads/sample/sample3/Shared.java diff --git a/src/Mainer.java b/src/Mainer.java index bb741e9..0874650 100755 --- a/src/Mainer.java +++ b/src/Mainer.java @@ -4,7 +4,11 @@ public class Mainer { public static void main(String[] args) { // MANDATORY - + String a = "sd"; + + String b = "sd"; + + System.out.println(!a.equals(b)); } diff --git a/src/loops/test.md b/src/loops/test.md new file mode 100644 index 0000000..781d938 --- /dev/null +++ b/src/loops/test.md @@ -0,0 +1,3 @@ +### Loops + +In class we've see \ No newline at end of file diff --git a/src/threads/sample/sample1/Mainer.java b/src/threads/sample/sample1/Mainer.java new file mode 100644 index 0000000..eb930e4 --- /dev/null +++ b/src/threads/sample/sample1/Mainer.java @@ -0,0 +1,32 @@ +package threads.sample.sample1; + +public class Mainer { + public static void main(String[] args) { + int num = 10; + + // Create 10 MyThread objects + // They will NOT start to run just yet + MyThread[] threads = new MyThread[num]; + for (int i = 0; i < threads.length; i++) { + threads[i] = new MyThread(); + } + + for(MyThread t : threads) { + t.start(); // we call start() even though we've overwritten "run()" + } + + for(MyThread t : threads) { + // the x.join() method will make the caller thread + // To wait until the thread 'x' will finish its work + try { + t.join(); // Prevents the main thread from running + // until 't' finishes its work + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + System.out.println("Main is done"); + + } +} diff --git a/src/threads/sample/sample1/MyThread.java b/src/threads/sample/sample1/MyThread.java new file mode 100644 index 0000000..52728f4 --- /dev/null +++ b/src/threads/sample/sample1/MyThread.java @@ -0,0 +1,28 @@ +package threads.sample.sample1; + +/*** + * Create your own Thread class by extending the java Thread class + * Or implementing the Runnable interface. + * After doing either of these, you have to override the run() method. + * ONLY code inside of the run() method will run on the separate thread + * + * + * For this example we're extending the Thread class + */ +public class MyThread extends Thread{ + + @Override + public void run() { + String threadName = Thread.currentThread().getName(); + System.out.println("Hi there my name is " + threadName); + + try { + // Pretend to do extensive work for 2 seconds + Thread.sleep(2 * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.println("Thread: " + threadName + " is done"); + } +} diff --git a/src/threads/sample/sample2/Mainer.java b/src/threads/sample/sample2/Mainer.java new file mode 100644 index 0000000..5b20e26 --- /dev/null +++ b/src/threads/sample/sample2/Mainer.java @@ -0,0 +1,34 @@ +package threads.sample.sample2; + +public class Mainer { + + public static void main(String[] args) { + + SharedData sharedData = new SharedData(); // only 1 data object created + + + // Create 10 MyThread objects + // They will NOT start to run just yet + MyThread[] threads = new MyThread[2]; + for (int i = 0; i < threads.length; i++) { + threads[i] = new MyThread(sharedData); + } + + for(MyThread t : threads) { + t.start(); // we call start() even though we've overwritten "run()" + } + + for(MyThread t : threads) { + // the x.join() method will make the caller thread + // To wait until the thread 'x' will finish its work + try { + t.join(); // Prevents the main thread from running + // until 't' finishes its work + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + System.out.println("Main is done: " + sharedData.n); + } +} diff --git a/src/threads/sample/sample2/MyThread.java b/src/threads/sample/sample2/MyThread.java new file mode 100644 index 0000000..4adb9f0 --- /dev/null +++ b/src/threads/sample/sample2/MyThread.java @@ -0,0 +1,21 @@ +package threads.sample.sample2; + +public class MyThread extends Thread { + private SharedData data; + + public MyThread(SharedData data) { + this.data = data; + } + + @Override + public void run() { + for (int i =0; i < 50; i++) { + // Synchronization happens on the OBJECT + synchronized (data) { + // Only 1 thread is allowed inside this chunk of code + // Incrementation is safe + this.data.n++; + } + } + } +} diff --git a/src/threads/sample/sample2/SharedData.java b/src/threads/sample/sample2/SharedData.java new file mode 100644 index 0000000..50b6515 --- /dev/null +++ b/src/threads/sample/sample2/SharedData.java @@ -0,0 +1,14 @@ +package threads.sample.sample2; + +public class SharedData { + // n is public + // and NOT thread safe + + public int n; + + public SharedData() { + this.n = 0; + } + + +} diff --git a/src/threads/sample/sample3/Shared.java b/src/threads/sample/sample3/Shared.java new file mode 100644 index 0000000..6653575 --- /dev/null +++ b/src/threads/sample/sample3/Shared.java @@ -0,0 +1,7 @@ +package threads.sample.sample3; + +// not complete yet +public class Shared { + + +}