Skip to content

Commit

Permalink
Added more strings exercises as well as one more advanced OOP one
Browse files Browse the repository at this point in the history
  • Loading branch information
shaqed committed Oct 10, 2018
1 parent 43c429d commit 82697b7
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Mainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand Down
3 changes: 3 additions & 0 deletions src/loops/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Loops

In class we've see
32 changes: 32 additions & 0 deletions src/threads/sample/sample1/Mainer.java
Original file line number Diff line number Diff line change
@@ -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");

}
}
28 changes: 28 additions & 0 deletions src/threads/sample/sample1/MyThread.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
34 changes: 34 additions & 0 deletions src/threads/sample/sample2/Mainer.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 21 additions & 0 deletions src/threads/sample/sample2/MyThread.java
Original file line number Diff line number Diff line change
@@ -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++;
}
}
}
}
14 changes: 14 additions & 0 deletions src/threads/sample/sample2/SharedData.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
7 changes: 7 additions & 0 deletions src/threads/sample/sample3/Shared.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package threads.sample.sample3;

// not complete yet
public class Shared {


}

0 comments on commit 82697b7

Please sign in to comment.